Skip to content

Commit c1c02f2

Browse files
authored
Ignore blake2 when not available (e.g. FIPS mode) (#706)
1 parent 65b387b commit c1c02f2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

tests/test_package.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_hash_manager():
214214
assert hasher.hexdigest() == TWINE_1_5_0_WHEEL_HEXDIGEST
215215

216216

217-
def test_fips_hash_manager(monkeypatch):
217+
def test_fips_hash_manager_md5(monkeypatch):
218218
"""Generate hexdigest without MD5 when hashlib is using FIPS mode."""
219219
replaced_md5 = pretend.raiser(ValueError("fipsmode"))
220220
monkeypatch.setattr(package_file.hashlib, "md5", replaced_md5)
@@ -226,6 +226,18 @@ def test_fips_hash_manager(monkeypatch):
226226
assert hasher.hexdigest() == hashes
227227

228228

229+
def test_fips_hash_manager_blake2(monkeypatch):
230+
"""Generate hexdigest without BLAKE2 when hashlib is using FIPS mode."""
231+
replaced_blake2b = pretend.raiser(ValueError("fipsmode"))
232+
monkeypatch.setattr(package_file.hashlib, "blake2b", replaced_blake2b)
233+
234+
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"
235+
hasher = package_file.HashManager(filename)
236+
hasher.hash()
237+
hashes = TWINE_1_5_0_WHEEL_HEXDIGEST._replace(blake2=None)
238+
assert hasher.hexdigest() == hashes
239+
240+
229241
def test_pkginfo_returns_no_metadata(monkeypatch):
230242
"""Raise an exception when pkginfo can't interpret the metadata.
231243

twine/package.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ def __init__(self, filename: str) -> None:
220220

221221
self._sha2_hasher = hashlib.sha256()
222222

223-
self._blake_hasher = hashlib.blake2b(digest_size=256 // 8)
223+
self._blake_hasher = None
224+
try:
225+
self._blake_hasher = hashlib.blake2b(digest_size=256 // 8)
226+
except ValueError:
227+
# FIPS mode disables blake2
228+
pass
224229

225230
def _md5_update(self, content: bytes) -> None:
226231
if self._md5_hasher is not None:

0 commit comments

Comments
 (0)