Skip to content

Commit f775df3

Browse files
authored
Merge pull request #1190 from cquick01/hashlib-fips-support
refactor: add 'usedforsecurity=False' arg to hashlib.md5 usage
2 parents 8ab4c3b + f08974e commit f775df3

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lark/lark.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from .exceptions import ConfigurationError, assert_config, UnexpectedInput
2020
from .utils import Serialize, SerializeMemoizer, FS, isascii, logger
21-
from .load_grammar import load_grammar, FromPackageLoader, Grammar, verify_used_files, PackageResource
21+
from .load_grammar import load_grammar, FromPackageLoader, Grammar, verify_used_files, PackageResource, md5_digest
2222
from .tree import Tree
2323
from .common import LexerConf, ParserConf, _ParserArgType, _LexerArgType
2424

@@ -301,7 +301,7 @@ def __init__(self, grammar: 'Union[Grammar, str, IO[str]]', **options) -> None:
301301
options_str = ''.join(k+str(v) for k, v in options.items() if k not in unhashable)
302302
from . import __version__
303303
s = grammar + options_str + __version__ + str(sys.version_info[:2])
304-
cache_md5 = hashlib.md5(s.encode('utf8')).hexdigest()
304+
cache_md5 = md5_digest(s)
305305

306306
if isinstance(self.options.cache, str):
307307
cache_fn = self.options.cache

lark/load_grammar.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ def do_import(self, dotted_path: Tuple[str, ...], base_path: Optional[str], alia
13121312
except IOError:
13131313
continue
13141314
else:
1315-
h = hashlib.md5(text.encode('utf8')).hexdigest()
1315+
h = md5_digest(text)
13161316
if self.used_files.get(joined_path, h) != h:
13171317
raise RuntimeError("Grammar file was changed during importing")
13181318
self.used_files[joined_path] = h
@@ -1391,7 +1391,7 @@ def verify_used_files(file_hashes):
13911391
if text is None: # We don't know how to load the path. ignore it.
13921392
continue
13931393

1394-
current = hashlib.md5(text.encode()).hexdigest()
1394+
current = md5_digest(text)
13951395
if old != current:
13961396
logger.info("File %r changed, rebuilding Parser" % path)
13971397
return False
@@ -1407,3 +1407,15 @@ def load_grammar(grammar, source, import_paths, global_keep_all_tokens):
14071407
builder = GrammarBuilder(global_keep_all_tokens, import_paths)
14081408
builder.load_grammar(grammar, source)
14091409
return builder.build(), builder.used_files
1410+
1411+
1412+
def md5_digest(s: str) -> str:
1413+
"""Get the md5 digest of a string
1414+
1415+
Supports the `usedforsecurity` argument for Python 3.9+ to allow running on
1416+
a FIPS-enabled system.
1417+
"""
1418+
if sys.version_info >= (3, 9):
1419+
return hashlib.md5(s.encode('utf8'), usedforsecurity=False).hexdigest()
1420+
else:
1421+
return hashlib.md5(s.encode('utf8')).hexdigest()

0 commit comments

Comments
 (0)