comparison cutils/util/__init__.py @ 184:6154b8e4ba94

Include some new algorithms: CRC
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 14 Jan 2025 11:26:38 +0100
parents e081b6ee5570
children f04d4b1c14b3
comparison
equal deleted inserted replaced
183:62436c255dc8 184:6154b8e4ba94
80 try: 80 try:
81 return hashlib.blake2s 81 return hashlib.blake2s
82 except AttributeError: 82 except AttributeError:
83 import pyblake2 83 import pyblake2
84 return pyblake2.blake2s 84 return pyblake2.blake2s
85
86
87 def get_crc(name):
88 """Get the factory for a CRC"""
89
90 from ..crcmod.predefined import PredefinedCrc
91
92 def _crc_type():
93 return PredefinedCrc(name)
94
95 return _crc_type
85 96
86 97
87 def argv2algo(s): 98 def argv2algo(s):
88 """Convert a command line algorithm specifier into a tuple with the 99 """Convert a command line algorithm specifier into a tuple with the
89 type/factory of the digest and the algorithms tag for output purposes. 100 type/factory of the digest and the algorithms tag for output purposes.
122 return (get_blake2s(), "BLAKE2s") 133 return (get_blake2s(), "BLAKE2s")
123 elif s in ("blake2-256", "blake2b-256"): 134 elif s in ("blake2-256", "blake2b-256"):
124 return (get_blake2b_256(), "BLAKE2b-256") 135 return (get_blake2b_256(), "BLAKE2b-256")
125 elif s == "md5": 136 elif s == "md5":
126 return (hashlib.md5, "MD5") 137 return (hashlib.md5, "MD5")
138 elif s in ("crc24", "crc-24",
139 "crc24-openpgp", "crc-24-openpgp"):
140 return (get_crc("crc-24"), "CRC-24")
141 elif s in ("crc32", "crc-32",
142 "crc32-pkzip", "crc-32-pkzip",
143 "crc32-iso", "crc-32-iso",
144 "crc32-iso-hdlc", "crc-32-iso-hdlc"):
145 return (get_crc("crc-32"), "CRC-32-ISO")
146 elif s in ("crc32-posix", "crc-32-posix",
147 "crc32-cksum", "crc-32-cksum",
148 "posix"):
149 return (get_crc("posix"), "CRC-32-POSIX")
150 elif s in ("crc64", "crc-64",
151 "crc64-iso", "crc-64-iso"):
152 return (get_crc("crc-64"), "CRC-64-ISO")
153 elif s in ("crc64-2", "crc-64-2",
154 "crc64-iso-2", "crc-64-iso-2",
155 "crc64-mcrc64", "crc-64-mcrc64"):
156 return (get_crc("crc-64-2"), "CRC-64-ISO-2")
157 elif s in ("crc64-ecma", "crc-64-ecma"):
158 return (get_crc("crc-64-ecma"), "CRC-64-ECMA")
159 elif s in ("crc64-xz", "crc-64-xz",
160 "crc64-go-ecma", "crc-64-go-ecma"):
161 return (get_crc("crc-64-xz"), "CRC-64-XZ")
162 elif s in ("crc64-go", "crc-64-go",
163 "crc64-go-iso", "crc-64-go-iso"):
164 return (get_crc("crc-64-go"), "CRC-64-GO-ISO")
165 elif s in ("crc64-redis", "crc-64-redis"):
166 return (get_crc("crc-64-redis"), "CRC-64-REDIS")
127 else: 167 else:
128 raise argparse.ArgumentTypeError( 168 raise argparse.ArgumentTypeError(
129 "`{}' is not a recognized algorithm".format(s)) 169 "`{}' is not a recognized algorithm".format(s))
130 170
131 171
164 return get_blake2s() 204 return get_blake2s()
165 elif s in ("BLAKE2b-256", "BLAKE2b256"): # also compat for openssl dgst 205 elif s in ("BLAKE2b-256", "BLAKE2b256"): # also compat for openssl dgst
166 return get_blake2b_256() 206 return get_blake2b_256()
167 elif s == "MD5": 207 elif s == "MD5":
168 return hashlib.md5 208 return hashlib.md5
209 elif s == "CRC-24":
210 return get_crc("crc-24")
211 elif s == "CRC-32-ISO":
212 return get_crc("crc-32")
213 elif s == "CRC-32-POSIX":
214 return get_crc("posix")
215 elif s == "CRC-64-ISO":
216 return get_crc("crc-64")
217 elif s == "CRC-64-ISO-2":
218 return get_crc("crc-64-2")
219 elif s == "CRC-64-ECMA":
220 return get_crc("crc-64-ecma")
221 elif s == "CRC-64-XZ":
222 return get_crc("crc-64-xz")
223 elif s == "CRC-64-GO-ISO":
224 return get_crc("crc-64-go")
225 elif s == "CRC-64-REDIS":
226 return get_crc("crc-64-redis")
169 else: 227 else:
170 raise ValueError("unknown algorithm: {}".format(s)) 228 raise ValueError("unknown algorithm: {}".format(s))
171 229
172 230
173 def normalize_filename(filename, strip_leading_dot_slash=False): 231 def normalize_filename(filename, strip_leading_dot_slash=False):