gh-103256: Fix hmac algorithm to support fallback implementation (gh-103286)

(cherry picked from commit efb0a2cf3a)

Co-authored-by: Dong-hee Na <donghee.na@python.org>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
Miss Islington (bot) 2023-04-06 19:27:46 -07:00 committed by GitHub
parent bbe04d9d1c
commit 8740fd855c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View file

@ -373,6 +373,16 @@ class TestVectorsTestCase(unittest.TestCase):
with self.assertRaisesRegex(TypeError, r'required.*digestmod'):
hmac.HMAC(key, msg=data, digestmod='')
def test_with_fallback(self):
cache = getattr(hashlib, '__builtin_constructor_cache')
try:
cache['foo'] = hashlib.sha256
hexdigest = hmac.digest(b'key', b'message', 'foo').hex()
expected = '6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a'
self.assertEqual(hexdigest, expected)
finally:
cache.pop('foo')
class ConstructorTestCase(unittest.TestCase):