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).
........
This commit is contained in:
Guido van Rossum 2007-11-06 20:51:31 +00:00
parent a3538ebfe3
commit a19f80c6df
3 changed files with 186 additions and 8 deletions

View file

@ -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()