hasher.py 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import sys
  2. import hashlib
  3. # Constants
  4. GENESIS_STR = 'This is the genesis hash'
  5. # Specify buffer size
  6. BUF_SIZE = 65536 # 64kB chunks!
  7. def hashfile(filename):
  8. sha256 = hashlib.sha256()
  9. with open(filename, 'rb') as f:
  10. while True:
  11. data = f.read(BUF_SIZE)
  12. if not data:
  13. break
  14. sha256.update(data)
  15. return sha256.hexdigest()
  16. def hashdata(data):
  17. sha256 = hashlib.sha256(data.encode())
  18. return sha256.hexdigest()
  19. # Constant!
  20. GENESIS_HASH = hashdata(GENESIS_STR)
  21. # sample txstring
  22. txstring = '[13e36dd45bc4115a7ddab608d9ca26fdb951d0ff74631c554f2c3653b58e8a31, datetime, 1, 3, 40304.50]'
  23. def gen_txhash(txstring):
  24. # Generates a transaction hash from
  25. # transaction data
  26. # Return new transaction hash
  27. return hashdata(txstring)
  28. def append_tx(txhash, pbhash):
  29. # Takes transaction hash and
  30. # appends to previous block hash
  31. inputs = f'[{pbhash}, {txhash}]'
  32. # Return new block hash
  33. return hashdata(inputs)