Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ def digest(key, msg, digest):


def _compute_digest_fallback(key, msg, digest):
if not isinstance(key, (bytes, bytearray)):
raise TypeError(f"key: expected bytes or bytearray, "
f"but got {type(key).__name__!r}")
digest_cons = _get_digest_constructor(digest)
if _is_shake_constructor(digest_cons):
raise ValueError(f"unsupported hash algorithm {digest}")
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,12 @@ def test_with_fallback(self):
finally:
cache.pop('foo')

@hashlib_helper.requires_hashdigest("md5")
def test_hmac_digest_reject_memoryview_for_key(self):
hmac = import_fresh_module("hmac", blocked=["_hashlib", "_hmac"])
with self.assertRaises(TypeError):
hmac.digest(memoryview(b"small"), b"world", "md5")

@hashlib_helper.requires_openssl_hashdigest("md5")
@bigmemtest(size=_4G + 5, memuse=2, dry_run=False)
def test_hmac_digest_overflow_error_openssl_only(self, size):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Make sure that the pure Python fallback of :func:`hmac.digest` reject
keys that are neither :class:`bytes` or :class:`bytearray`. Previously,
an :exc:`AttributeError` was raised instead of :exc:`TypeError`.
Patch by Bénédikt Tran.
Loading