From b2084d37d694df2803756375b62beb2b1ade8b80 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 May 2018 12:26:10 -0500 Subject: [PATCH 1/4] tests: add new tests for uhashlib differences .. these tests currently fail, but a subsequent commit will fix them --- tests/extmod/uhashlib_sha1.py | 7 +++++++ tests/extmod/uhashlib_sha256.py | 7 +++++++ 2 files changed, 14 insertions(+) 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()) From 005226ae548a45bde03a2b2400998185aa68495e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 May 2018 12:28:36 -0500 Subject: [PATCH 2/4] uhashlib: some functions should refuse unicode for python3 compatibility .. this maybe should be subject to MICROPY_CPYTHON_COMPAT, except that is not defined in the main circuitpython ports so it would be a change that makes no difference. --- extmod/moduhashlib.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 3fad69247e10b..7599b8496421e 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); From 11a97bdffeedb933939514a7e6e27ac8796cb978 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 May 2018 12:29:55 -0500 Subject: [PATCH 3/4] uhashlib: masquerade as hashlib for python3 compatibility --- extmod/moduhashlib.c | 2 +- py/objmodule.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 7599b8496421e..7ae42a15f1eaa 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -149,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) }, From 2955ada22f5d12a7b4cb9c1881ad71bb7bfe1fa4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 6 May 2018 12:23:21 -0500 Subject: [PATCH 4/4] docs: fix references to uhashlib --- docs/library/{uhashlib.rst => hashlib.rst} | 10 +++++----- docs/library/index.rst | 2 +- shared-bindings/index.rst | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) rename docs/library/{uhashlib.rst => hashlib.rst} (91%) 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/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** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy