-
-
Notifications
You must be signed in to change notification settings - Fork 954
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pass usedforsecurity to hashlib.md5 to fix error on FIPS-enabled…
- Loading branch information
1 parent
58a104e
commit 0aef172
Showing
2 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import hashlib | ||
|
||
# Compat wrapper to always include the `usedforsecurity=...` parameter, | ||
# which is only added from Python 3.9 onwards. | ||
# We use this flag to indicate that we use `md5` hashes only for non-security | ||
# cases (our ETag checksums). | ||
# If we don't indicate that we're using MD5 for non-security related reasons, | ||
# then attempting to use this function will raise an error when used | ||
# environments which enable a strict "FIPs mode". | ||
# | ||
# See issue: https://github.com/encode/starlette/issues/1365 | ||
try: | ||
|
||
hashlib.md5(b"data", usedforsecurity=True) # type: ignore[call-arg] | ||
|
||
def md5_hexdigest( | ||
data: bytes, *, usedforsecurity: bool = True | ||
) -> str: # pragma: no cover | ||
return hashlib.md5( # type: ignore[call-arg] | ||
data, usedforsecurity=usedforsecurity | ||
).hexdigest() | ||
|
||
except TypeError: # pragma: no cover | ||
|
||
def md5_hexdigest(data: bytes, *, usedforsecurity: bool = True) -> str: | ||
return hashlib.md5(data).hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters