Skip to content

extmod/moduhashlib: allow multiple calls to digest() method #6850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions extmod/moduhashlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, 32);
mbedtls_sha256_finish_ret((mbedtls_sha256_context *)&self->state, (unsigned char *)vstr.buf);
mbedtls_sha256_context tmp_ctx = *(mbedtls_sha256_context *)self->state;
mbedtls_sha256_finish_ret(&tmp_ctx, (unsigned char *)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}

Expand Down Expand Up @@ -129,7 +130,8 @@ STATIC mp_obj_t uhashlib_sha256_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA256_BLOCK_SIZE);
sha256_final((CRYAL_SHA256_CTX *)self->state, (byte *)vstr.buf);
CRYAL_SHA256_CTX tmp_ctx = *(CRYAL_SHA256_CTX *)self->state;
sha256_final(&tmp_ctx, (byte *)vstr.buf);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
#endif
Expand Down
20 changes: 10 additions & 10 deletions tests/extmod/uhashlib_sha256.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
# 56 bytes is a boundary case in the algorithm
print(hashlib.sha256(b"\xff" * 56).digest())

# TODO: running .digest() several times in row is not supported()
# h = hashlib.sha256(b'123')
# print(h.digest())
# print(h.digest())

# TODO: partial digests are not supported
# h = hashlib.sha256(b'123')
# print(h.digest())
# h.update(b'456')
# print(h.digest())
# running .digest() several times in row is now supported
h = hashlib.sha256(b'123')
print(h.digest())
print(h.digest())

# partial digests are supported
h = hashlib.sha256(b'123')
print(h.digest())
h.update(b'456')
print(h.digest())