Guest User

Untitled

a guest
Jan 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import hashlib
  2. import json
  3.  
  4. class TRBlock(object) :
  5.  
  6. def __init__(self, index, timestamp, data, prevHash, nonce, target):
  7. '''
  8. Default constructor for creating a block.
  9.  
  10. Parameters
  11. ----------
  12. index : int
  13. The index of the block
  14. timestamp: long
  15. Timestamp in epoch, number of seconds since 1 Jan 1970
  16. data : object
  17. The actual data that needs to be stored on block chain
  18. prevHash : str
  19. The hash of the previous block
  20. nonce : str
  21. The nonce which has been mined
  22. target: int
  23. Number of leading zeros
  24. '''
  25. self.index = index
  26. self.timestamp = timestamp
  27. self.data = data
  28. self.prevHash = prevHash
  29. self.nonce = nonce
  30. self.target = target
  31.  
  32. def compute_hash(self):
  33. '''
  34. Compute sha1 hash and convert it into hexadecimal string, json.dumps
  35. uses ":" separator without space
  36.  
  37. Returns
  38. -------
  39. str
  40. sha1 hash of the attributes converted into hexadecimal string
  41. '''
  42. return hashlib.sha1(json.dumps(self.__dict__, separators=(',', ':')) \
  43. .encode("utf-8")).hexdigest()
  44.  
  45.  
  46. if __name__ == "__main__":
  47. block = TRBlock(0,1514528022,"Hello World", "000000000000000000000000000", \
  48. "Random nonce",8)
  49. print(block.compute_hash())
Add Comment
Please, Sign In to add comment