Fixes Issue 1385: The hmac module now computes the correct hmac when using
hashes with a block size other than 64 bytes (such as sha384 and sha512).
diff --git a/Lib/hmac.py b/Lib/hmac.py
index 88c3fd5..729ef38 100644
--- a/Lib/hmac.py
+++ b/Lib/hmac.py
@@ -3,6 +3,8 @@
Implements the HMAC algorithm as described by RFC 2104.
"""
+import warnings as _warnings
+
trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
@@ -16,7 +18,7 @@
_secret_backdoor_key = []
class HMAC:
- """RFC2104 HMAC class.
+ """RFC 2104 HMAC class. Also complies with RFC 4231.
This supports the API for Cryptographic Hash Functions (PEP 247).
"""
@@ -48,7 +50,21 @@
self.inner = self.digest_cons()
self.digest_size = self.inner.digest_size
- blocksize = self.blocksize
+ if hasattr(self.inner, 'block_size'):
+ blocksize = self.inner.block_size
+ if blocksize < 16:
+ # Very low blocksize, most likely a legacy value like
+ # Lib/sha.py and Lib/md5.py have.
+ _warnings.warn('block_size of %d seems too small; using our '
+ 'default of %d.' % (blocksize, self.blocksize),
+ RuntimeWarning, 2)
+ blocksize = self.blocksize
+ else:
+ _warnings.warn('No block_size attribute on given digest object; '
+ 'Assuming %d.' % (self.blocksize),
+ RuntimeWarning, 2)
+ blocksize = self.blocksize
+
if len(key) > blocksize:
key = self.digest_cons(key).digest()