Skip to content

Instantly share code, notes, and snippets.

@mDuo13
Created October 30, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mDuo13/fe9563c2a385cfe9024e4c60107ea4fb to your computer and use it in GitHub Desktop.
Save mDuo13/fe9563c2a385cfe9024e4c60107ea4fb to your computer and use it in GitHub Desktop.
SHA-512Half tool
# Commandline tool for calculating "SHA-512Half" hashes of arbitrary strings
#!/bin/env python3
import argparse
from hashlib import sha512
p = argparse.ArgumentParser(description='Calculate SHA-512Half value.')
p.add_argument('preimage', type=str, help='String to hash')
p.add_argument('--lower', '-l', action="store_true", help='Use lowercase letters a-f')
p.add_argument("--nullprefix", "-u", action="store_true", help="Prefix with a null (\\x00) character")
# The "null prefix" thing helps for XRP Ledger hashes which cast ASCII characters to UInt16s before hashing.
# https://github.com/ripple/rippled/blob/c6b6d82a754fe449cc533e18659df483c10a5c98/src/ripple/protocol/impl/Indexes.cpp#L48
a = p.parse_args()
instr = a.preimage.encode("utf-8")
if a.nullprefix:
instr = b'\x00' + instr
val = sha512(instr).hexdigest()[:64]
if a.lower:
print(val.lower())
else:
print(val.upper())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment