diff --git a/docs/library/uhashlib.rst b/docs/library/hashlib.rst similarity index 91% rename from docs/library/uhashlib.rst rename to docs/library/hashlib.rst index d0a3422a213d6..0205d5e6a8c72 100644 --- a/docs/library/uhashlib.rst +++ b/docs/library/hashlib.rst @@ -1,9 +1,9 @@ -:mod:`uhashlib` -- hashing algorithms +:mod:`hashlib` -- hashing algorithms ===================================== .. include:: ../templates/unsupported_in_circuitpython.inc -.. module:: uhashlib +.. module:: hashlib :synopsis: hashing algorithms |see_cpython_module| :mod:`cpython:hashlib`. @@ -29,15 +29,15 @@ be implemented: Constructors ------------ -.. class:: uhashlib.sha256([data]) +.. class:: hashlib.sha256([data]) Create an SHA256 hasher object and optionally feed ``data`` into it. -.. class:: uhashlib.sha1([data]) +.. class:: hashlib.sha1([data]) Create an SHA1 hasher object and optionally feed ``data`` into it. -.. class:: uhashlib.md5([data]) +.. class:: hashlib.md5([data]) Create an MD5 hasher object and optionally feed ``data`` into it. diff --git a/docs/library/index.rst b/docs/library/index.rst index 804bff93c4790..466068640d275 100644 --- a/docs/library/index.rst +++ b/docs/library/index.rst @@ -23,7 +23,7 @@ Python standard libraries and micro-libraries binascii.rst ucollections.rst uerrno.rst - uhashlib.rst + hashlib.rst uheapq.rst uio.rst ujson.rst diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 3fad69247e10b..7ae42a15f1eaa 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -36,6 +36,14 @@ #include "lib/axtls/crypto/crypto.h" #endif +static void check_not_unicode(const mp_obj_t arg) { +#if MICROPY_CPYTHON_COMPAT + if (MP_OBJ_IS_STR(arg)) { + mp_raise_TypeError("a bytes-like object is required"); + } +#endif +} + typedef struct _mp_obj_hash_t { mp_obj_base_t base; char state[0]; @@ -70,6 +78,7 @@ STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n #endif STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) { + check_not_unicode(arg); mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); @@ -80,6 +89,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update); #if MICROPY_PY_UHASHLIB_SHA1 STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) { + check_not_unicode(arg); mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ); @@ -139,7 +149,7 @@ STATIC const mp_obj_type_t sha1_type = { #endif STATIC const mp_rom_map_elem_t mp_module_hashlib_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uhashlib) }, + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_hashlib) }, { MP_ROM_QSTR(MP_QSTR_sha256), MP_ROM_PTR(&sha256_type) }, #if MICROPY_PY_UHASHLIB_SHA1 { MP_ROM_QSTR(MP_QSTR_sha1), MP_ROM_PTR(&sha1_type) }, diff --git a/py/objmodule.c b/py/objmodule.c index 0394d7cbb65eb..3bea196444002 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -199,7 +199,7 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = { { MP_ROM_QSTR(MP_QSTR_utimeq), MP_ROM_PTR(&mp_module_utimeq) }, #endif #if MICROPY_PY_UHASHLIB - { MP_ROM_QSTR(MP_QSTR_uhashlib), MP_ROM_PTR(&mp_module_uhashlib) }, + { MP_ROM_QSTR(MP_QSTR_hashlib), MP_ROM_PTR(&mp_module_uhashlib) }, #endif #if MICROPY_PY_UBINASCII { MP_ROM_QSTR(MP_QSTR_binascii), MP_ROM_PTR(&mp_module_ubinascii) }, diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index 38e26191d2ba6..76b5e340b864f 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -29,6 +29,7 @@ Module Supported Ports `busio` **All Supported** `digitalio` **All Supported** `gamepad` **SAMD Express, nRF** +`hashlib` **ESP8266** `math` **All Supported** `microcontroller` **All Supported** `multiterminal` **ESP8266** diff --git a/tests/extmod/uhashlib_sha1.py b/tests/extmod/uhashlib_sha1.py index 4f7066899aa11..9d6427b33ff9f 100644 --- a/tests/extmod/uhashlib_sha1.py +++ b/tests/extmod/uhashlib_sha1.py @@ -19,3 +19,10 @@ sha1 = hashlib.sha1(b'hello') sha1.update(b'world') print(sha1.digest()) + +sha1 = hashlib.sha1(b'hello') +try: + sha1.update(u'world') +except TypeError as e: + print("TypeError") +print(sha1.digest()) diff --git a/tests/extmod/uhashlib_sha256.py b/tests/extmod/uhashlib_sha256.py index 3200e8f5cd3d5..0322c20de4a4e 100644 --- a/tests/extmod/uhashlib_sha256.py +++ b/tests/extmod/uhashlib_sha256.py @@ -23,6 +23,13 @@ print(hashlib.sha256(b"\xff" * 64).digest()) +sha256 = hashlib.sha256(b'hello') +try: + sha256.update(u'world') +except TypeError as e: + print("TypeError") +print(sha256.digest()) + # TODO: running .digest() several times in row is not supported() #h = hashlib.sha256(b'123') #print(h.digest())
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: