hasher.py 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. # Init objects
  8. sha256 = hashlib.sha256()
  9. def hashfile(filename):
  10. with open(filename, 'rb') as f:
  11. while True:
  12. data = f.read(BUF_SIZE)
  13. if not data:
  14. break
  15. sha256.update(data)
  16. return sha256.hexdigest
  17. def hashdata(data):
  18. sha256 = hashlib.sha256(data.encode())
  19. return sha1.hexdigest
  20. # Constant!
  21. GENESIS_HASH = hashdata(GENESIS_STR)
  22. # sample txstring
  23. txstring = '[13e36dd45bc4115a7ddab608d9ca26fdb951d0ff74631c554f2c3653b58e8a31, datetime, 1, 3, 40304.50]'
  24. def gen_txhash(txstring):
  25. # Generates a transaction hash from
  26. # transaction data
  27. # Return new transaction hash
  28. return hashdata(txstring)
  29. def append_tx(txhash, pbhash):
  30. # Takes transaction hash and
  31. # appends to previous block hash
  32. inputs = f'[{pbhash}, {txhash}]'
  33. # Return new block hash
  34. return hashdata(inputs)