Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Created June 30, 2020 01:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save oconnor663/595719be4f0d1668fd971e3020db8d44 to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
import sys
from hashlib import sha256
def ten_digits_from_u64(n):
assert len(str(2**64)) == 20
assert n < 2**64
s = "{:020}".format(n)
return s[-10:]
def main():
assert ten_digits_from_u64(123) == "0000000123"
assert ten_digits_from_u64(1_000_000_000_123) == "0000000123"
hash_state = sha256()
buf = memoryview(bytearray(1 << 16))
for n in iter(lambda: sys.stdin.buffer.readinto(buf), 0):
hash_state.update(buf[:n])
hash_bytes = hash_state.digest()
assert len(hash_bytes) == 32
for i in range(4):
int_bytes = hash_bytes[i * 8:(i + 1) * 8]
int_val = int.from_bytes(int_bytes, "little")
digits = ten_digits_from_u64(int_val)
print(digits, end="")
print()
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment