comparison 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
comparison
equal deleted inserted replaced
171:7912be4930b4 172:804a823c63f5
12 "argv2algo", 12 "argv2algo",
13 "algotag2algotype", 13 "algotag2algotype",
14 "get_blake2b", 14 "get_blake2b",
15 "get_blake2b_256", 15 "get_blake2b_256",
16 "get_blake2s", 16 "get_blake2s",
17 "default_algotag",
17 "fsencode", 18 "fsencode",
18 ] 19 ]
19 20
20 21
21 import argparse 22 import argparse
23 import os 24 import os
24 import sys 25 import sys
25 26
26 27
27 PY2 = sys.version_info[0] < 3 28 PY2 = sys.version_info[0] < 3
29
30
31 def default_algotag():
32 """Determine the "best" default algorithm.
33
34 Depend on availability in :mod:`hashlib`.
35
36 Prefer BLAKE2b-256, SHA256 or SHA1 -- in this order.
37
38 Does not consider :mod:`pyblake2` if it is available eventually.
39
40 """
41 if "blake2b" in hashlib.algorithms_available:
42 return "BLAKE2b-256"
43 if "sha256" in hashlib.algorithms_available:
44 return "SHA256"
45 return "SHA1"
28 46
29 47
30 def get_blake2b(): 48 def get_blake2b():
31 """Get the factory for blake2b""" 49 """Get the factory for blake2b"""
32 try: 50 try:
66 84
67 def argv2algo(s): 85 def argv2algo(s):
68 """Convert a command line algorithm specifier into a tuple with the 86 """Convert a command line algorithm specifier into a tuple with the
69 type/factory of the digest and the algorithms tag for output purposes. 87 type/factory of the digest and the algorithms tag for output purposes.
70 88
71 :param str s: the specifier from the commane line 89 :param str s: the specifier from the command line; should include all
90 algorithm tags also (for proper round-tripping)
72 :return: the internal digest specification 91 :return: the internal digest specification
73 :rtype: a tuple (digest_type_or_factory, name_in_output) 92 :rtype: a tuple (digest_type_or_factory, name_in_output)
74 :raises argparse.ArgumentTypeError: for unrecognized algorithms or names 93 :raises argparse.ArgumentTypeError: for unrecognized algorithms or names
75 94
76 String comparisons are done case-insensitively. 95 String comparisons are done case-insensitively.