cpython: 71f4a805d262 (original) (raw)
--- a/Doc/library/hmac.rst +++ b/Doc/library/hmac.rst @@ -79,6 +79,25 @@ An HMAC object has the following methods compute the digests of strings that share a common initial substring. +A hash object has the following attributes: + +.. attribute:: HMAC.digest_size +
+.. attribute:: HMAC.block_size +
+ This module also provides the following helper function: .. function:: compare_digest(a, b)
--- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -70,6 +70,10 @@ class HMAC: RuntimeWarning, 2) blocksize = self.blocksize
# self.blocksize is the default blocksize. self.block_size is[](#l2.7)
# effective block size as well as the public API attribute.[](#l2.8)
self.block_size = blocksize[](#l2.9)
+ if len(key) > blocksize: key = self.digest_cons(key).digest() @@ -79,6 +83,10 @@ class HMAC: if msg is not None: self.update(msg)
+ def update(self, msg): """Update this hashing object with the string msg. """
--- a/Lib/test/test_hmac.py +++ b/Lib/test/test_hmac.py @@ -12,8 +12,16 @@ class TestVectorsTestCase(unittest.TestC def md5test(key, data, digest): h = hmac.HMAC(key, data, digestmod=hashlib.md5) self.assertEqual(h.hexdigest().upper(), digest.upper())
self.assertEqual(h.name, "hmac-md5")[](#l3.7)
self.assertEqual(h.digest_size, 16)[](#l3.8)
self.assertEqual(h.block_size, 64)[](#l3.9)
+ h = hmac.HMAC(key, data, digestmod='md5') self.assertEqual(h.hexdigest().upper(), digest.upper())
self.assertEqual(h.name, "hmac-md5")[](#l3.13)
self.assertEqual(h.digest_size, 16)[](#l3.14)
self.assertEqual(h.block_size, 64)[](#l3.15)
+ md5test(b"\x0b" * 16, b"Hi There", @@ -48,8 +56,15 @@ class TestVectorsTestCase(unittest.TestC def shatest(key, data, digest): h = hmac.HMAC(key, data, digestmod=hashlib.sha1) self.assertEqual(h.hexdigest().upper(), digest.upper())
self.assertEqual(h.name, "hmac-sha1")[](#l3.24)
self.assertEqual(h.digest_size, 20)[](#l3.25)
self.assertEqual(h.block_size, 64)[](#l3.26)
+ h = hmac.HMAC(key, data, digestmod='sha1') self.assertEqual(h.hexdigest().upper(), digest.upper())
self.assertEqual(h.name, "hmac-sha1")[](#l3.30)
self.assertEqual(h.digest_size, 20)[](#l3.31)
self.assertEqual(h.block_size, 64)[](#l3.32)
shatest(b"\x0b" * 20, @@ -81,12 +96,20 @@ class TestVectorsTestCase(unittest.TestC b"and Larger Than One Block-Size Data"), "e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
- def _rfc4231_test_cases(self, hashfunc, hash_name, digest_size, block_size): def hmactest(key, data, hexdigests):
hmac_name = "hmac-" + hash_name[](#l3.43) h = hmac.HMAC(key, data, digestmod=hashfunc)[](#l3.44) self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])[](#l3.45)
h = hmac.HMAC(key, data, digestmod=hashname)[](#l3.46)
self.assertEqual(h.name, hmac_name)[](#l3.47)
self.assertEqual(h.digest_size, digest_size)[](#l3.48)
self.assertEqual(h.block_size, block_size)[](#l3.49)
h = hmac.HMAC(key, data, digestmod=hash_name)[](#l3.51) self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])[](#l3.52)
self.assertEqual(h.name, hmac_name)[](#l3.53)
self.assertEqual(h.digest_size, digest_size)[](#l3.54)
self.assertEqual(h.block_size, block_size)[](#l3.55)
# 4.2. Test Case 1 @@ -197,16 +220,16 @@ class TestVectorsTestCase(unittest.TestC }) def test_sha224_rfc4231(self):
self._rfc4231_test_cases(hashlib.sha224, 'sha224')[](#l3.63)
self._rfc4231_test_cases(hashlib.sha224, 'sha224', 28, 64)[](#l3.64)
def test_sha256_rfc4231(self):
self._rfc4231_test_cases(hashlib.sha256, 'sha256')[](#l3.67)
self._rfc4231_test_cases(hashlib.sha256, 'sha256', 32, 64)[](#l3.68)
def test_sha384_rfc4231(self):
self._rfc4231_test_cases(hashlib.sha384, 'sha384')[](#l3.71)
self._rfc4231_test_cases(hashlib.sha384, 'sha384', 48, 128)[](#l3.72)
def test_sha512_rfc4231(self):
self._rfc4231_test_cases(hashlib.sha512, 'sha512')[](#l3.75)
self._rfc4231_test_cases(hashlib.sha512, 'sha512', 64, 128)[](#l3.76)
def test_legacy_block_size_warnings(self): class MockCrazyHash(object):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -59,6 +59,9 @@ Core and Builtins Library ------- +- Issue #18775: Add name and block_size attribute to HMAC object. They now