Merge pull request #806 from jepler/hashlib-py3

Rename uhashlib->hashlib, improve python3 compatibility
This commit is contained in:
Scott Shawcroft 2018-05-06 22:36:52 -07:00 committed by GitHub
commit d42c83ff41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 8 deletions

View File

@ -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.

View File

@ -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

View File

@ -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) },

View File

@ -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) },

View File

@ -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**

View File

@ -19,3 +19,10 @@ except AttributeError:
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())

View File

@ -23,6 +23,13 @@ print(h.digest())
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())