From 13a888d725e03922825a51505d8ea7de27737992 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 21 Jul 2021 19:03:10 +0200 Subject: [PATCH 1/2] hashlib: import pure python hash first to unbreak the hmac module With the current import strategy that favours the uhashlib implementation before using the "internal" hashlib version external modules like "hmac" break because the uhashlib versions lack properties like `{digest,block}_size, copy()`. This commit changes the import so that the internal version of the hash is tried to be imported first and only if there is no internal version then the uhashlib version is used. --- python-stdlib/hashlib/hashlib/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/python-stdlib/hashlib/hashlib/__init__.py b/python-stdlib/hashlib/hashlib/__init__.py index d7afbf819..86fd8d42f 100644 --- a/python-stdlib/hashlib/hashlib/__init__.py +++ b/python-stdlib/hashlib/hashlib/__init__.py @@ -6,11 +6,18 @@ def init(): for i in ("sha1", "sha224", "sha256", "sha384", "sha512"): - c = getattr(uhashlib, i, None) - if not c: + # first try to import the python-stdlib hash so that we are compatible + # with e.g. hmac (uhashlib lacks {digest,block}_size, copy()) + try: c = __import__("_" + i, None, None, (), 1) c = getattr(c, i) - globals()[i] = c + except ImportError: + c = None + # fallback to uhashlib (e.g. sha1) + if not c: + c = getattr(uhashlib, i, None) + if c: + globals()[i] = c init() From b1b351a44766a5bd4562e405ecf90079066aa9c9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 21 Jul 2021 20:00:06 +0200 Subject: [PATCH 2/2] hashlib: add test for sha1 as well (for completeness) --- python-stdlib/hashlib/test_hashlib.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python-stdlib/hashlib/test_hashlib.py b/python-stdlib/hashlib/test_hashlib.py index 6cb687399..812248ae2 100644 --- a/python-stdlib/hashlib/test_hashlib.py +++ b/python-stdlib/hashlib/test_hashlib.py @@ -9,6 +9,11 @@ import hashlib patterns = [ + ( + "sha1", + b"1234", + b"q\x10\xed\xa4\xd0\x9e\x06*\xa5\xe4\xa3\x90\xb0\xa5r\xac\r,\x02\x20", + ), ( "sha224", b"1234",