mirror of
https://github.com/python/cpython.git
synced 2025-11-25 12:44:13 +00:00
Issue #17276: MD5 as default digestmod for HMAC is deprecated. The HMAC
module supports digestmod names, e.g. hmac.HMAC('sha1').
This commit is contained in:
parent
7f48396cb5
commit
634919a9fa
9 changed files with 57 additions and 20 deletions
|
|
@ -10,7 +10,9 @@ class TestVectorsTestCase(unittest.TestCase):
|
|||
# Test the HMAC module against test vectors from the RFC.
|
||||
|
||||
def md5test(key, data, digest):
|
||||
h = hmac.HMAC(key, data)
|
||||
h = hmac.HMAC(key, data, digestmod=hashlib.md5)
|
||||
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
||||
h = hmac.HMAC(key, data, digestmod='md5')
|
||||
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
||||
|
||||
md5test(b"\x0b" * 16,
|
||||
|
|
@ -46,6 +48,9 @@ class TestVectorsTestCase(unittest.TestCase):
|
|||
def shatest(key, data, digest):
|
||||
h = hmac.HMAC(key, data, digestmod=hashlib.sha1)
|
||||
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
||||
h = hmac.HMAC(key, data, digestmod='sha1')
|
||||
self.assertEqual(h.hexdigest().upper(), digest.upper())
|
||||
|
||||
|
||||
shatest(b"\x0b" * 20,
|
||||
b"Hi There",
|
||||
|
|
@ -76,10 +81,13 @@ class TestVectorsTestCase(unittest.TestCase):
|
|||
b"and Larger Than One Block-Size Data"),
|
||||
"e8e99d0f45237d786d6bbaa7965c7808bbff1a91")
|
||||
|
||||
def _rfc4231_test_cases(self, hashfunc):
|
||||
def _rfc4231_test_cases(self, hashfunc, hashname):
|
||||
def hmactest(key, data, hexdigests):
|
||||
h = hmac.HMAC(key, data, digestmod=hashfunc)
|
||||
self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
|
||||
h = hmac.HMAC(key, data, digestmod=hashname)
|
||||
self.assertEqual(h.hexdigest().lower(), hexdigests[hashfunc])
|
||||
|
||||
|
||||
# 4.2. Test Case 1
|
||||
hmactest(key = b'\x0b'*20,
|
||||
|
|
@ -189,16 +197,16 @@ class TestVectorsTestCase(unittest.TestCase):
|
|||
})
|
||||
|
||||
def test_sha224_rfc4231(self):
|
||||
self._rfc4231_test_cases(hashlib.sha224)
|
||||
self._rfc4231_test_cases(hashlib.sha224, 'sha224')
|
||||
|
||||
def test_sha256_rfc4231(self):
|
||||
self._rfc4231_test_cases(hashlib.sha256)
|
||||
self._rfc4231_test_cases(hashlib.sha256, 'sha256')
|
||||
|
||||
def test_sha384_rfc4231(self):
|
||||
self._rfc4231_test_cases(hashlib.sha384)
|
||||
self._rfc4231_test_cases(hashlib.sha384, 'sha384')
|
||||
|
||||
def test_sha512_rfc4231(self):
|
||||
self._rfc4231_test_cases(hashlib.sha512)
|
||||
self._rfc4231_test_cases(hashlib.sha512, 'sha512')
|
||||
|
||||
def test_legacy_block_size_warnings(self):
|
||||
class MockCrazyHash(object):
|
||||
|
|
@ -222,6 +230,13 @@ class TestVectorsTestCase(unittest.TestCase):
|
|||
hmac.HMAC(b'a', b'b', digestmod=MockCrazyHash)
|
||||
self.fail('Expected warning about small block_size')
|
||||
|
||||
def test_with_digestmod_warning(self):
|
||||
with self.assertWarns(PendingDeprecationWarning):
|
||||
key = b"\x0b" * 16
|
||||
data = b"Hi There"
|
||||
digest = "9294727A3638BB1C13F48EF8158BFC9D"
|
||||
h = hmac.HMAC(key, data)
|
||||
self.assertEqual(h.hexdigest().upper(), digest)
|
||||
|
||||
|
||||
class ConstructorTestCase(unittest.TestCase):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue