Skip to content

Commit

Permalink
Fix md5 hashing with FIPS mode (Project-MONAI#6635)
Browse files Browse the repository at this point in the history
### Description
MD5 hashing is not allowed in FIPS enabled machines for security. A
simple fix is to use the `usedforsecurity=False` flag in the `md5()`
calls.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Matthew Vine <[email protected]>
Signed-off-by: monai-bot <[email protected]>
Co-authored-by: monai-bot <[email protected]>
  • Loading branch information
MattTheCuber and monai-bot authored Jun 21, 2023
1 parent 2cbed6c commit e475196
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.8
python: python3

ci:
autofix_prs: true
Expand Down
17 changes: 15 additions & 2 deletions monai/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import math
import os
import pickle
import sys
from collections import abc, defaultdict
from collections.abc import Generator, Iterable, Mapping, Sequence, Sized
from copy import deepcopy
Expand Down Expand Up @@ -1370,7 +1371,13 @@ def json_hashing(item) -> bytes:
"""
# TODO: Find way to hash transforms content as part of the cache
cache_key = hashlib.md5(json.dumps(item, sort_keys=True).encode("utf-8")).hexdigest()
cache_key = ""
if sys.version_info.minor < 9:
cache_key = hashlib.md5(json.dumps(item, sort_keys=True).encode("utf-8")).hexdigest()
else:
cache_key = hashlib.md5(
json.dumps(item, sort_keys=True).encode("utf-8"), usedforsecurity=False # type: ignore
).hexdigest()
return f"{cache_key}".encode()


Expand All @@ -1385,7 +1392,13 @@ def pickle_hashing(item, protocol=pickle.HIGHEST_PROTOCOL) -> bytes:
Returns: the corresponding hash key
"""
cache_key = hashlib.md5(pickle.dumps(sorted_dict(item), protocol=protocol)).hexdigest()
cache_key = ""
if sys.version_info.minor < 9:
cache_key = hashlib.md5(pickle.dumps(sorted_dict(item), protocol=protocol)).hexdigest()
else:
cache_key = hashlib.md5(
pickle.dumps(sorted_dict(item), protocol=protocol), usedforsecurity=False # type: ignore
).hexdigest()
return f"{cache_key}".encode()


Expand Down

0 comments on commit e475196

Please sign in to comment.