Skip to content

Commit 9bee955

Browse files
committed
expose 1-shot HMAC
1 parent 09c631a commit 9bee955

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Lib/hmac.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
compare_digest = _hashopenssl.compare_digest
1414
_functype = type(_hashopenssl.openssl_sha256) # builtin type
1515

16+
try:
17+
import _hmac
18+
except ImportError:
19+
_hmac = None
20+
_functype = None
21+
else:
22+
_functype = type(_hmac.compute_md5) # builtin type
23+
1624
import hashlib as _hashlib
1725

1826
trans_5C = bytes((x ^ 0x5C) for x in range(256))
@@ -196,6 +204,12 @@ def digest(key, msg, digest):
196204
A hashlib constructor returning a new hash object. *OR*
197205
A module supporting PEP 247.
198206
"""
207+
if _hmac is not None and isinstance(digest, (str, _functype)):
208+
try:
209+
return _hmac.compute_digest(key, msg, digest)
210+
except (OverflowError, _hashopenssl.UnsupportedDigestmodError):
211+
pass
212+
199213
if _hashopenssl is not None and isinstance(digest, (str, _functype)):
200214
try:
201215
return _hashopenssl.hmac_digest(key, msg, digest)

0 commit comments

Comments
 (0)