def mixed_case_checksum(data): o = "" checksum = keccak.Keccak256(data.hex().encode("ascii")).digest() v = parse_256(checksum) for i, c in enumerate(data.hex()): if c in "0123456789": o += c else: o += c.upper() if (v & (2**(255 - 4 * i))) else c.lower() return "0x" + o
assert h.digest() == MD else: assert Squeezed.startswith(h.digest()) def process_katfile(fn, mkhasher): data = {} for f in open(fn): if len(f.strip()) == 0 or f[0] == '#': continue lhs, rhs = f.strip().split(' = ', 1) data[lhs] = rhs if lhs in triggers: if longinp in data: process_kat_long(mkhasher, **data) else: process_kat_short(mkhasher, **data) data = {} def run_glob(pat, mkhasher): for g in glob.glob(pat): print ('Processing:', g) process_katfile(g, mkhasher) if __name__ == '__main__': run_glob('KeccakKAT/*MsgKAT_0.txt', lambda: keccak.KeccakHash(1024, 576, 1024)) run_glob('KeccakKAT/*MsgKAT_224.txt', lambda: keccak.Keccak224()) run_glob('KeccakKAT/*MsgKAT_256.txt', lambda: keccak.Keccak256()) run_glob('KeccakKAT/*MsgKAT_384.txt', lambda: keccak.Keccak384()) run_glob('KeccakKAT/*MsgKAT_512.txt', lambda: keccak.Keccak512())
def private_eth_to_public(priv): secret_key = parse_256(bytearray.fromhex(priv)) public_key = (ecdsa.generator_secp256k1 * secret_key).pair() data = ser_256(public_key[0]) + ser_256(public_key[1]) keyhash = keccak.Keccak256(data).digest() return mixed_case_checksum(keyhash[-20:])
def hwif_to_eth_account(hwif): testnet, depth, parent_fingerprint, child_index, chain_code, secret_key, public_key = from_hwif( hwif) data = ser_256(public_key[0]) + ser_256(public_key[1]) keyhash = keccak.Keccak256(data).digest() return mixed_case_checksum(keyhash[-20:])