Skip to content

Commit fea4cdf

Browse files
committed
pythongh-99108: Refactor _sha256 & _sha512 into _sha2.
This merges their code. They're backed by the same single HACL* star static library, having them be a single module simplifies maintenance. This should unbreak the wasm enscripten builds that currently fail due to linking in --whole-archive mode and the HACL* library appearing twice. Long unnoticed error fixed: `_sha512.SHA384Type` was doubly assigned and was actually SHA512Type. Nobody depends on those internal names.
1 parent d777790 commit fea4cdf

17 files changed

+1303
-1497
lines changed

Lib/hashlib.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ def __get_builtin_constructor(name):
9292
import _md5
9393
cache['MD5'] = cache['md5'] = _md5.md5
9494
elif name in {'SHA256', 'sha256', 'SHA224', 'sha224'}:
95-
import _sha256
96-
cache['SHA224'] = cache['sha224'] = _sha256.sha224
97-
cache['SHA256'] = cache['sha256'] = _sha256.sha256
95+
import _sha2
96+
cache['SHA224'] = cache['sha224'] = _sha2.sha224
97+
cache['SHA256'] = cache['sha256'] = _sha2.sha256
9898
elif name in {'SHA512', 'sha512', 'SHA384', 'sha384'}:
99-
import _sha512
100-
cache['SHA384'] = cache['sha384'] = _sha512.sha384
101-
cache['SHA512'] = cache['sha512'] = _sha512.sha512
99+
import _sha2
100+
cache['SHA384'] = cache['sha384'] = _sha2.sha384
101+
cache['SHA512'] = cache['sha512'] = _sha2.sha512
102102
elif name in {'blake2b', 'blake2s'}:
103103
import _blake2
104104
cache['blake2b'] = _blake2.blake2b

Lib/test/test_hashlib.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,10 @@ def add_builtin_constructor(name):
172172
_sha1 = self._conditional_import_module('_sha1')
173173
if _sha1:
174174
add_builtin_constructor('sha1')
175-
_sha256 = self._conditional_import_module('_sha256')
176-
if _sha256:
175+
_sha2 = self._conditional_import_module('_sha2')
176+
if _sha2:
177177
add_builtin_constructor('sha224')
178178
add_builtin_constructor('sha256')
179-
_sha512 = self._conditional_import_module('_sha512')
180-
if _sha512:
181179
add_builtin_constructor('sha384')
182180
add_builtin_constructor('sha512')
183181
if _blake2:
@@ -371,19 +369,20 @@ def check(self, name, data, hexdigest, shake=False, **kwargs):
371369
# 2 is for hashlib.name(...) and hashlib.new(name, ...)
372370
self.assertGreaterEqual(len(constructors), 2)
373371
for hash_object_constructor in constructors:
374-
m = hash_object_constructor(data, **kwargs)
375-
computed = m.hexdigest() if not shake else m.hexdigest(length)
376-
self.assertEqual(
377-
computed, hexdigest,
378-
"Hash algorithm %s constructed using %s returned hexdigest"
379-
" %r for %d byte input data that should have hashed to %r."
380-
% (name, hash_object_constructor,
381-
computed, len(data), hexdigest))
382-
computed = m.digest() if not shake else m.digest(length)
383-
digest = bytes.fromhex(hexdigest)
384-
self.assertEqual(computed, digest)
385-
if not shake:
386-
self.assertEqual(len(digest), m.digest_size)
372+
with self.subTest(implementation=hash_object_constructor):
373+
m = hash_object_constructor(data, **kwargs)
374+
computed = m.hexdigest() if not shake else m.hexdigest(length)
375+
self.assertEqual(
376+
computed, hexdigest,
377+
"Hash algorithm %s constructed using %s returned hexdigest"
378+
" %r for %d byte input data that should have hashed to %r."
379+
% (name, hash_object_constructor,
380+
computed, len(data), hexdigest))
381+
computed = m.digest() if not shake else m.digest(length)
382+
digest = bytes.fromhex(hexdigest)
383+
self.assertEqual(computed, digest)
384+
if not shake:
385+
self.assertEqual(len(digest), m.digest_size)
387386

388387
if not shake and kwargs.get("key") is None:
389388
# skip shake and blake2 extended parameter tests
@@ -404,14 +403,15 @@ def check_file_digest(self, name, data, hexdigest):
404403

405404
try:
406405
for digest in digests:
407-
buf = io.BytesIO(data)
408-
buf.seek(0)
409-
self.assertEqual(
410-
hashlib.file_digest(buf, digest).hexdigest(), hexdigest
411-
)
412-
with open(os_helper.TESTFN, "rb") as f:
413-
digestobj = hashlib.file_digest(f, digest)
414-
self.assertEqual(digestobj.hexdigest(), hexdigest)
406+
with self.subTest(msg="check_file_digest", implementation=digest):
407+
buf = io.BytesIO(data)
408+
buf.seek(0)
409+
self.assertEqual(
410+
hashlib.file_digest(buf, digest).hexdigest(), hexdigest
411+
)
412+
with open(os_helper.TESTFN, "rb") as f:
413+
digestobj = hashlib.file_digest(f, digest)
414+
self.assertEqual(digestobj.hexdigest(), hexdigest)
415415
finally:
416416
os.unlink(os_helper.TESTFN)
417417

Makefile.pre.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -2635,9 +2635,8 @@ MODULE__HASHLIB_DEPS=$(srcdir)/Modules/hashlib.h
26352635
MODULE__IO_DEPS=$(srcdir)/Modules/_io/_iomodule.h
26362636
MODULE__MD5_DEPS=$(srcdir)/Modules/hashlib.h
26372637
MODULE__SHA1_DEPS=$(srcdir)/Modules/hashlib.h
2638-
MODULE__SHA256_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) $(LIBHACL_A)
2638+
MODULE__SHA2_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) $(LIBHACL_A)
26392639
MODULE__SHA3_DEPS=$(srcdir)/Modules/_sha3/sha3.c $(srcdir)/Modules/_sha3/sha3.h $(srcdir)/Modules/hashlib.h
2640-
MODULE__SHA512_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) $(LIBHACL_A)
26412640
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
26422641
MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data.h $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
26432642
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The built-in extension modules for :mod:`hashlib` SHA2 algorithms, used when
2+
OpenSSL does provide them, now live in a single internal ``_sha2`` module
3+
instead of separate ``_sha256`` and ``_sha512`` modules.

Modules/Setup

+1-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ PYTHONPATH=$(COREPYTHONPATH)
165165
#_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c
166166
#_md5 md5module.c
167167
#_sha1 sha1module.c
168-
#_sha256 sha256module.c
169-
#_sha512 sha512module.c
168+
#_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Streaming_SHA2.a
170169
#_sha3 _sha3/sha3module.c
171170

172171
# text encodings and unicode

Modules/Setup.stdlib.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@
7979
# hashing builtins, can be disabled with --without-builtin-hashlib-hashes
8080
@MODULE__MD5_TRUE@_md5 md5module.c
8181
@MODULE__SHA1_TRUE@_sha1 sha1module.c
82-
@MODULE__SHA256_TRUE@_sha256 sha256module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Streaming_SHA2.a
83-
@MODULE__SHA512_TRUE@_sha512 sha512module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Streaming_SHA2.a
82+
@MODULE__SHA2_TRUE@_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Streaming_SHA2.a
8483
@MODULE__SHA3_TRUE@_sha3 _sha3/sha3module.c
8584
@MODULE__BLAKE2_TRUE@_blake2 _blake2/blake2module.c _blake2/blake2b_impl.c _blake2/blake2s_impl.c
8685

Modules/clinic/sha256module.c.h

-225
This file was deleted.

0 commit comments

Comments
 (0)