comparison cutils/shasum.py @ 85:d445534b80bb

Get the factory for blake2b and blake2s indirectly: prepare for blake2-256
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 20 Apr 2022 23:15:55 +0200
parents 163de6dd6e05
children fd1cfd1b0f9d
comparison
equal deleted inserted replaced
84:163de6dd6e05 85:d445534b80bb
376 opts.algorithm[0], 376 opts.algorithm[0],
377 mo.group(2), 377 mo.group(2),
378 mo.group(1)) 378 mo.group(1))
379 else: 379 else:
380 return None 380 return None
381
382
383 def get_blake2b():
384 """Get the factory for blake2b"""
385 return hashlib.blake2b
386
387
388 def get_blake2s():
389 """Get the factory for blake2s"""
390 return hashlib.blake2s
381 391
382 392
383 def argv2algo(s): 393 def argv2algo(s):
384 """Convert a command line algorithm specifier into a tuple with the 394 """Convert a command line algorithm specifier into a tuple with the
385 type/factory of the digest and the algorithms tag for output purposes. 395 type/factory of the digest and the algorithms tag for output purposes.
409 elif s in ("3-384", "sha3-384"): 419 elif s in ("3-384", "sha3-384"):
410 return (hashlib.sha3_384, "SHA3-384") 420 return (hashlib.sha3_384, "SHA3-384")
411 elif s in ("3-512", "sha3-512"): 421 elif s in ("3-512", "sha3-512"):
412 return (hashlib.sha3_512, "SHA3-512") 422 return (hashlib.sha3_512, "SHA3-512")
413 elif s in ("blake2b", "blake2b-512"): 423 elif s in ("blake2b", "blake2b-512"):
414 return (hashlib.blake2b, "BLAKE2b") 424 return (get_blake2b(), "BLAKE2b")
415 elif s in ("blake2s", "blake2s-256"): 425 elif s in ("blake2s", "blake2s-256"):
416 return (hashlib.blake2s, "BLAKE2s") 426 return (get_blake2s(), "BLAKE2s")
417 elif s == "md5": 427 elif s == "md5":
418 return (hashlib.md5, "MD5") 428 return (hashlib.md5, "MD5")
419 else: 429 else:
420 raise argparse.ArgumentTypeError( 430 raise argparse.ArgumentTypeError(
421 "`{}' is not a recognized algorithm".format(s)) 431 "`{}' is not a recognized algorithm".format(s))
448 elif s == "SHA3-384": 458 elif s == "SHA3-384":
449 return hashlib.sha3_384 459 return hashlib.sha3_384
450 elif s == "SHA3-512": 460 elif s == "SHA3-512":
451 return hashlib.sha3_512 461 return hashlib.sha3_512
452 elif s == "BLAKE2b": 462 elif s == "BLAKE2b":
453 return hashlib.blake2b 463 return get_blake2b()
454 elif s == "BLAKE2s": 464 elif s == "BLAKE2s":
455 return hashlib.blake2s 465 return get_blake2s()
456 elif s == "MD5": 466 elif s == "MD5":
457 return hashlib.md5 467 return hashlib.md5
458 else: 468 else:
459 raise ValueError("unknown algorithm: {}".format(s)) 469 raise ValueError("unknown algorithm: {}".format(s))
460 470