diff cutils/util/__init__.py @ 172:804a823c63f5

Now the selection of the default algorithm depends on availiability in hashlib
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 10 Jan 2025 11:38:31 +0100
parents ed45abb4940f
children e081b6ee5570
line wrap: on
line diff
--- a/cutils/util/__init__.py	Fri Jan 10 11:25:56 2025 +0100
+++ b/cutils/util/__init__.py	Fri Jan 10 11:38:31 2025 +0100
@@ -14,6 +14,7 @@
            "get_blake2b",
            "get_blake2b_256",
            "get_blake2s",
+           "default_algotag",
            "fsencode",
            ]
 
@@ -27,6 +28,23 @@
 PY2 = sys.version_info[0] < 3
 
 
+def default_algotag():
+    """Determine the "best" default algorithm.
+
+    Depend on availability in :mod:`hashlib`.
+
+    Prefer BLAKE2b-256, SHA256 or SHA1 -- in this order.
+
+    Does not consider :mod:`pyblake2` if it is available eventually.
+
+    """
+    if "blake2b" in hashlib.algorithms_available:
+        return "BLAKE2b-256"
+    if "sha256" in hashlib.algorithms_available:
+        return "SHA256"
+    return "SHA1"
+
+
 def get_blake2b():
     """Get the factory for blake2b"""
     try:
@@ -68,7 +86,8 @@
     """Convert a command line algorithm specifier into a tuple with the
     type/factory of the digest and the algorithms tag for output purposes.
 
-    :param str s: the specifier from the commane line
+    :param str s: the specifier from the command line; should include all
+                  algorithm tags also (for proper round-tripping)
     :return: the internal digest specification
     :rtype: a tuple (digest_type_or_factory, name_in_output)
     :raises argparse.ArgumentTypeError: for unrecognized algorithms or names