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):
--- /dev/null
+Fixed a bug that caused :mod:`hmac` to raise an exception when the requested
+hash algorithm was not available in OpenSSL despite being available
+separately as part of ``hashlib`` itself. It now falls back properly to the
+built-in. This could happen when, for example, your OpenSSL does not include
+SHA3 support and you want to compute ``hmac.digest(b'K', b'M',
+'sha3_256')``.
}
}
if (digest == NULL) {
- _setException(PyExc_ValueError, "unsupported hash type %s", name);
+ _setException(state->unsupported_digestmod_error, "unsupported hash type %s", name);
return NULL;
}
return digest;