diff options
author | Guido van Rossum <guido@python.org> | 2007-11-06 20:51:31 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-11-06 20:51:31 (GMT) |
commit | a19f80c6df2df5e8a5d0cff37131097835ef971e (patch) | |
tree | 21509dce5dd4cf63c75df00e83b411f43db479aa /Lib/hmac.py | |
parent | a3538ebfe3120cf7478f91bd76674234b8cffda8 (diff) | |
download | cpython-a19f80c6df2df5e8a5d0cff37131097835ef971e.zip cpython-a19f80c6df2df5e8a5d0cff37131097835ef971e.tar.gz cpython-a19f80c6df2df5e8a5d0cff37131097835ef971e.tar.bz2 |
Merged revisions 58862-58885 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r58868 | gregory.p.smith | 2007-11-05 16:19:03 -0800 (Mon, 05 Nov 2007) | 3 lines
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.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/Lib/hmac.py b/Lib/hmac.py index 1911689..6f2ae2e 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 = bytes((x ^ 0x5C) for x in range(256)) trans_36 = bytes((x ^ 0x36) for x in range(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). """ @@ -52,7 +54,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() |