Skip to content

Commit 8f81cc0

Browse files
committed
Add support for FIPS
Reported-by: RayGozer @RayGozer Reference: aboutcode-org/scancode-toolkit#3165 Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent d1c5e9d commit 8f81cc0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/commoncode/hash.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import binascii
1010
import hashlib
11+
import sys
12+
from functools import partial
1113

1214
from commoncode.codec import bin_to_num
1315
from commoncode.codec import urlsafe_b64encode
@@ -51,12 +53,21 @@ def intdigest(self):
5153
return hasher
5254

5355

56+
# for FIPS support
57+
sys_v0 = sys.version_info[0]
58+
sys_v1 = sys.version_info[1]
59+
if sys_v0 == 3 and sys_v1 >= 9:
60+
md5_hasher = partial(hashlib.md5, usedforsecurity=False)
61+
else:
62+
md5_hasher = hashlib.md5
63+
64+
5465
# Base hashers for each bit size
5566
_hashmodules_by_bitsize = {
5667
# md5-based
57-
32: _hash_mod(32, hashlib.md5),
58-
64: _hash_mod(64, hashlib.md5),
59-
128: _hash_mod(128, hashlib.md5),
68+
32: _hash_mod(32, md5_hasher),
69+
64: _hash_mod(64, md5_hasher),
70+
128: _hash_mod(128, md5_hasher),
6071
# sha-based
6172
160: _hash_mod(160, hashlib.sha1),
6273
256: _hash_mod(256, hashlib.sha256),

0 commit comments

Comments
 (0)