summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2020-05-02 14:22:11 -0400
committerEli Collins <elic@assurancetechnologies.com>2020-05-02 14:22:11 -0400
commite1c53136ececf6068351d526907e751f9aebed39 (patch)
tree1b9067a366377d6af79752f1f558189131cce85d
parent18aa5a99271908054b8fc65d79c91c8404d486af (diff)
downloadpasslib-e1c53136ececf6068351d526907e751f9aebed39.tar.gz
passlib.crypto.digests: added hardcoded fallback info for common hashes;
so stats can be gathered retreived even if hash isn't available (e.g. FIPS mode)
-rw-r--r--passlib/crypto/digest.py35
-rw-r--r--passlib/tests/test_crypto_digest.py5
2 files changed, 40 insertions, 0 deletions
diff --git a/passlib/crypto/digest.py b/passlib/crypto/digest.py
index c811fd2..acfb9bf 100644
--- a/passlib/crypto/digest.py
+++ b/passlib/crypto/digest.py
@@ -96,6 +96,37 @@ _known_hash_names = [
("ripemd160", "ripemd-160", "ripemd"),
]
+
+#: dict mapping hashlib names to hardcoded digest info;
+#: so this is available even when hashes aren't present.
+_fallback_info = {
+ # name: (digest_size, block_size)
+ 'blake2b': (64, 128),
+ 'blake2s': (32, 64),
+ 'md4': (16, 64),
+ 'md5': (16, 64),
+ 'sha1': (20, 64),
+ 'sha224': (28, 64),
+ 'sha256': (32, 64),
+ 'sha384': (48, 128),
+ 'sha3_224': (28, 144),
+ 'sha3_256': (32, 136),
+ 'sha3_384': (48, 104),
+ 'sha3_512': (64, 72),
+ 'sha512': (64, 128),
+ 'shake128': (16, 168),
+ 'shake256': (32, 136),
+}
+
+
+def _gen_fallback_info():
+ out = {}
+ for alg in sorted(hashlib.algorithms_available | {"md4"}):
+ info = lookup_hash(alg)
+ out[info.name] = (info.digest_size, info.block_size)
+ return out
+
+
#: cache of hash info instances used by lookup_hash()
_hash_info_cache = {}
@@ -437,6 +468,10 @@ class HashInfo(SequenceMixin):
const()
self.error_text = msg
self.const = const
+ try:
+ self.digest_size, self.block_size = _fallback_info[name]
+ except KeyError:
+ pass
# handle "constructor not available" case
if const is None:
diff --git a/passlib/tests/test_crypto_digest.py b/passlib/tests/test_crypto_digest.py
index f948e39..461d209 100644
--- a/passlib/tests/test_crypto_digest.py
+++ b/passlib/tests/test_crypto_digest.py
@@ -129,6 +129,7 @@ class HashInfoTest(TestCase):
self.assertRaisesRegex(UnknownHashError, "unknown hash: 'xxx256'", info.const)
self.assertEqual(info.name, "xxx256")
self.assertEqual(info.digest_size, None)
+ self.assertEqual(info.block_size, None)
# should cache stub records
info2 = lookup_hash("xxx256", required=False)
@@ -155,6 +156,10 @@ class HashInfoTest(TestCase):
self.assertRegex(info.error_text, pat)
self.assertRaisesRegex(UnknownHashError, pat, info.const)
+ # should use hardcoded fallback info
+ self.assertEqual(info.digest_size, 16)
+ self.assertEqual(info.block_size, 64)
+
def test_lookup_hash_metadata(self):
"""lookup_hash() -- metadata"""