summaryrefslogtreecommitdiffstats
path: root/Lib/hmac.py
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2007-11-06 00:19:03 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2007-11-06 00:19:03 (GMT)
commite1ac4f19301aa0a4d28fc25b91c71c477c37cb21 (patch)
treef73016435d33c9db6aff9667061b79ff92057718 /Lib/hmac.py
parent91f9429bc34aa8879fd65fcbcbe81b5e923fc8cb (diff)
downloadcpython-e1ac4f19301aa0a4d28fc25b91c71c477c37cb21.zip
cpython-e1ac4f19301aa0a4d28fc25b91c71c477c37cb21.tar.gz
cpython-e1ac4f19301aa0a4d28fc25b91c71c477c37cb21.tar.bz2
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).
Diffstat (limited to 'Lib/hmac.py')
-rw-r--r--Lib/hmac.py20
1 files changed, 18 insertions, 2 deletions
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 @@ digest_size = None
_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 @@ class HMAC:
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()