Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/shasum.py @ 87:b46673c42894
Implement support for BLAKE2b-256 (aka BLAKE2-256).
Also implement some compatibility for OpenSSL's dgst tags.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 21 Apr 2022 00:09:17 +0200 |
| parents | fd1cfd1b0f9d |
| children | f69353f26937 |
comparison
equal
deleted
inserted
replaced
| 86:fd1cfd1b0f9d | 87:b46673c42894 |
|---|---|
| 53 aparser = argparse.ArgumentParser( | 53 aparser = argparse.ArgumentParser( |
| 54 description="Python implementation of shasum", | 54 description="Python implementation of shasum", |
| 55 fromfile_prefix_chars='@') | 55 fromfile_prefix_chars='@') |
| 56 aparser.add_argument( | 56 aparser.add_argument( |
| 57 "--algorithm", "-a", action="store", type=argv2algo, | 57 "--algorithm", "-a", action="store", type=argv2algo, |
| 58 help="1 (default), 224, 256, 384, 512, 3-224, 3-256, 3-384, 3-512, blake2b, blake2s, blake2, md5") | 58 help="1 (default), 224, 256, 384, 512, 3-224, 3-256, 3-384, 3-512, blake2b, blake2s, blake2, blake2-256, md5") |
| 59 aparser.add_argument( | 59 aparser.add_argument( |
| 60 "--base64", action="store_true", | 60 "--base64", action="store_true", |
| 61 help="Output checksums in base64 notation, not hexadecimal (OpenBSD).") | 61 help="Output checksums in base64 notation, not hexadecimal (OpenBSD).") |
| 62 aparser.add_argument( | 62 aparser.add_argument( |
| 63 "--binary", "-b", action="store_false", dest="text_mode", default=False, | 63 "--binary", "-b", action="store_false", dest="text_mode", default=False, |
| 386 | 386 |
| 387 | 387 |
| 388 def get_blake2s(): | 388 def get_blake2s(): |
| 389 """Get the factory for blake2s""" | 389 """Get the factory for blake2s""" |
| 390 return hashlib.blake2s | 390 return hashlib.blake2s |
| 391 | |
| 392 | |
| 393 def get_blake2_256(): | |
| 394 """Get the factory for blake2-256""" | |
| 395 | |
| 396 def _get_blake(): | |
| 397 return hashlib.blake2b(digest_size=32) | |
| 398 | |
| 399 return _get_blake | |
| 391 | 400 |
| 392 | 401 |
| 393 def argv2algo(s): | 402 def argv2algo(s): |
| 394 """Convert a command line algorithm specifier into a tuple with the | 403 """Convert a command line algorithm specifier into a tuple with the |
| 395 type/factory of the digest and the algorithms tag for output purposes. | 404 type/factory of the digest and the algorithms tag for output purposes. |
| 422 return (hashlib.sha3_512, "SHA3-512") | 431 return (hashlib.sha3_512, "SHA3-512") |
| 423 elif s in ("blake2b", "blake2b-512", "blake2", "blake2-512"): | 432 elif s in ("blake2b", "blake2b-512", "blake2", "blake2-512"): |
| 424 return (get_blake2b(), "BLAKE2b") | 433 return (get_blake2b(), "BLAKE2b") |
| 425 elif s in ("blake2s", "blake2s-256"): | 434 elif s in ("blake2s", "blake2s-256"): |
| 426 return (get_blake2s(), "BLAKE2s") | 435 return (get_blake2s(), "BLAKE2s") |
| 436 elif s in ("blake2-256", "blake2b-256"): | |
| 437 return (get_blake2_256(), "BLAKE2b-256") | |
| 427 elif s == "md5": | 438 elif s == "md5": |
| 428 return (hashlib.md5, "MD5") | 439 return (hashlib.md5, "MD5") |
| 429 else: | 440 else: |
| 430 raise argparse.ArgumentTypeError( | 441 raise argparse.ArgumentTypeError( |
| 431 "`{}' is not a recognized algorithm".format(s)) | 442 "`{}' is not a recognized algorithm".format(s)) |
| 457 return hashlib.sha3_256 | 468 return hashlib.sha3_256 |
| 458 elif s == "SHA3-384": | 469 elif s == "SHA3-384": |
| 459 return hashlib.sha3_384 | 470 return hashlib.sha3_384 |
| 460 elif s == "SHA3-512": | 471 elif s == "SHA3-512": |
| 461 return hashlib.sha3_512 | 472 return hashlib.sha3_512 |
| 462 elif s == "BLAKE2b": | 473 elif s in ("BLAKE2b", "BLAKE2b-512", "BLAKE2b512"): # compat for openssl |
| 463 return get_blake2b() | 474 return get_blake2b() |
| 464 elif s == "BLAKE2s": | 475 elif s in ("BLAKE2s", "BLAKE2s-256", "BLAKE2s256"): # compat for openssl |
| 465 return get_blake2s() | 476 return get_blake2s() |
| 477 elif s in ("BLAKE2b-256", "BLAKE2b256"): # also compat for openssl dgst | |
| 478 return get_blake2_256() | |
| 466 elif s == "MD5": | 479 elif s == "MD5": |
| 467 return hashlib.md5 | 480 return hashlib.md5 |
| 468 else: | 481 else: |
| 469 raise ValueError("unknown algorithm: {}".format(s)) | 482 raise ValueError("unknown algorithm: {}".format(s)) |
| 470 | 483 |
