diff cutils/shasum.py @ 87:b46673c42894

Implement support for BLAKE2b-256 (aka BLAKE2-256). Also implement some compatibility for OpenSSL's dgst tags.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 21 Apr 2022 00:09:17 +0200
parents fd1cfd1b0f9d
children f69353f26937
line wrap: on
line diff
--- a/cutils/shasum.py	Wed Apr 20 23:20:24 2022 +0200
+++ b/cutils/shasum.py	Thu Apr 21 00:09:17 2022 +0200
@@ -55,7 +55,7 @@
         fromfile_prefix_chars='@')
     aparser.add_argument(
         "--algorithm", "-a", action="store", type=argv2algo,
-        help="1 (default), 224, 256, 384, 512, 3-224, 3-256, 3-384, 3-512, blake2b, blake2s, blake2, md5")
+        help="1 (default), 224, 256, 384, 512, 3-224, 3-256, 3-384, 3-512, blake2b, blake2s, blake2, blake2-256, md5")
     aparser.add_argument(
         "--base64", action="store_true",
         help="Output checksums in base64 notation, not hexadecimal (OpenBSD).")
@@ -390,6 +390,15 @@
     return hashlib.blake2s
 
 
+def get_blake2_256():
+    """Get the factory for blake2-256"""
+
+    def _get_blake():
+        return hashlib.blake2b(digest_size=32)
+
+    return _get_blake
+
+
 def argv2algo(s):
     """Convert a command line algorithm specifier into a tuple with the
     type/factory of the digest and the algorithms tag for output purposes.
@@ -424,6 +433,8 @@
         return (get_blake2b(), "BLAKE2b")
     elif s in ("blake2s", "blake2s-256"):
         return (get_blake2s(), "BLAKE2s")
+    elif s in ("blake2-256", "blake2b-256"):
+        return (get_blake2_256(), "BLAKE2b-256")
     elif s == "md5":
         return (hashlib.md5, "MD5")
     else:
@@ -459,10 +470,12 @@
         return hashlib.sha3_384
     elif s == "SHA3-512":
         return hashlib.sha3_512
-    elif s == "BLAKE2b":
+    elif s in ("BLAKE2b", "BLAKE2b-512", "BLAKE2b512"):  # compat for openssl
         return get_blake2b()
-    elif s == "BLAKE2s":
+    elif s in ("BLAKE2s", "BLAKE2s-256", "BLAKE2s256"):  # compat for openssl
         return get_blake2s()
+    elif s in ("BLAKE2b-256", "BLAKE2b256"):   # also compat for openssl dgst
+        return get_blake2_256()
     elif s == "MD5":
         return hashlib.md5
     else: