Skip to content

Commit e8c0df6

Browse files
committed
Add support for FIPS
Reported-by: RayGozer @RayGozer Reference: #3165 Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 5112b25 commit e8c0df6

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/licensedcode/detection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import sys
1212
import os
1313
import logging
14-
import hashlib
1514
import uuid
1615
from enum import Enum
16+
from hashlib import sha1
1717
from collections import Counter
1818

1919
import attr
@@ -278,7 +278,7 @@ def identifier(self):
278278

279279
# Return a uuid generated from the contents of the matches
280280
identifier_string = repr(tuple(data))
281-
md_hash = hashlib.md5()
281+
md_hash = sha1()
282282
md_hash.update(identifier_string.encode('utf-8'))
283283
return str(uuid.UUID(md_hash.hexdigest()))
284284

src/licensedcode/match_hash.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99

1010
from array import array
11-
from hashlib import md5
11+
from hashlib import sha1
1212

1313

1414
from licensedcode.match import LicenseMatch
@@ -46,7 +46,7 @@ def tokens_hash(tokens):
4646
Return a digest binary string computed from a sequence of numeric token ids.
4747
"""
4848
as_bytes = array('h', tokens).tobytes()
49-
return md5(as_bytes).digest()
49+
return sha1(as_bytes).digest()
5050

5151

5252
def index_hash(rule_tokens):

src/licensedcode/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10-
import hashlib
1110
import os
1211
import sys
1312
import traceback
1413
from collections import Counter
1514
from collections import defaultdict
15+
from hashlib import sha1
1616
from itertools import chain
1717
from operator import itemgetter
1818
from os.path import abspath
@@ -2001,13 +2001,13 @@ def _from_expression(cls, license_expression=None, identifier=None, **kwargs):
20012001
def compute_unique_id(self):
20022002
"""
20032003
Return a a unique id string based on this rule content. (Today this is
2004-
an MD5 checksum of the text, but that's an implementation detail)
2004+
an SHA1 checksum of the text, but that's an implementation detail)
20052005
"""
20062006
if not self.text:
20072007
text = "None"
20082008
else:
20092009
text = self.text
2010-
return hashlib.md5(text.encode('utf-8')).hexdigest()
2010+
return sha1(text.encode('utf-8')).hexdigest()
20112011

20122012
def load_data(self, rule_file):
20132013
"""

src/packagedcode/cocoapods.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import json
1212
import os
1313
import logging
14+
from functools import partial
1415

1516
import saneyaml
1617
from packageurl import PackageURL
@@ -28,6 +29,7 @@
2829

2930
TRACE = os.environ.get('SCANCODE_DEBUG_PACKAGE', False)
3031

32+
3133
def logger_debug(*args):
3234
pass
3335

@@ -89,8 +91,10 @@ def get_hashed_path(name):
8991
Returns a string with a part of the file path derived from the md5 hash.
9092
9193
From https://github.com/CocoaPods/cdn.cocoapods.org:
92-
"There are a set of known prefixes for all Podspec paths, you take the name of the pod,
93-
create a hash (using md5) of it and take the first three characters."
94+
"There are a set of known prefixes for all Podspec paths, you take the
95+
name of the pod, create a hash (using md5) of it and take the first
96+
three characters."
97+
9498
"""
9599
if not name:
96100
return
@@ -105,8 +109,22 @@ def get_hashed_path(name):
105109
return hashed_path
106110

107111

112+
# for FIPS support
113+
sys_v0 = sys.version_info[0]
114+
sys_v1 = sys.version_info[1]
115+
if sys_v0 == 3 and sys_v1 >= 9:
116+
md5_hasher = partial(hashlib.md5, usedforsecurity=False)
117+
else:
118+
md5_hasher = hashlib.md5
119+
120+
108121
def get_first_three_md5_hash_characters(podname):
109-
return hashlib.md5(podname.encode('utf-8')).hexdigest()[0:3]
122+
"""
123+
From https://github.com/CocoaPods/cdn.cocoapods.org:
124+
"There are a set of known prefixes for all Podspec paths, you take the name of the pod,
125+
create a hash (using md5) of it and take the first three characters."
126+
"""
127+
return md5_hasher(podname.encode('utf-8')).hexdigest()[0:3]
110128

111129

112130
class BasePodHandler(models.DatafileHandler):

0 commit comments

Comments
 (0)