changeset 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 7912be4930b4
children e081b6ee5570
files cutils/treesum.py cutils/util/__init__.py
diffstat 2 files changed, 28 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Fri Jan 10 11:25:56 2025 +0100
+++ b/cutils/treesum.py	Fri Jan 10 11:38:31 2025 +0100
@@ -40,12 +40,14 @@
         """
         gp.add_argument(
             "--algorithm", "-a", action="store", type=util.argv2algo,
-            help="1 (aka sha1), 224, 256, 384, 512, "
+            help="1 (aka sha1), 224, 256 (aka sha256), 384, 512 (aka sha512), "
                  "3 (alias for sha3-512), 3-224, 3-256, 3-384, 3-512, "
-                 "blake2b, blake2b-256 (default), blake2s, "
+                 "blake2b, blake2b-256, blake2s, "
                  "blake2 (alias for blake2b), "
                  "blake2-256 (alias for blake2b-256), "
-                 "md5")
+                 "md5. "
+                 "The default depends on availability in hashlib: "
+                 "blake2b-256, sha256 or sha1.")
         gp.add_argument(
             "--append-output", action="store_true", dest="append_output",
             help="Append to the output file instead of overwriting it.")
@@ -205,7 +207,7 @@
 
 
 def gen_generate_opts(directories=[],
-                      algorithm="BLAKE2b-256",
+                      algorithm=util.default_algotag(),
                       append_output=False,
                       base64=False,
                       comment=[],
@@ -221,8 +223,7 @@
                       size_only=False):
     opts = argparse.Namespace(
         directories=directories,
-        algorithm=(util.algotag2algotype(algorithm),
-                   algorithm),
+        algorithm=util.argv2algo(algorithm),
         append_output=append_output,
         base64=base64,
         comment=comment,
@@ -251,7 +252,7 @@
 def generate_treesum(opts):
     # Provide defaults
     if not opts.algorithm:
-        opts.algorithm = util.argv2algo("blake2b-256")
+        opts.algorithm = util.argv2algo(util.default_algotag())
     if not opts.directories:
         opts.directories.append(".")
 
--- 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