fork download
  1. import hashlib
  2.  
  3. class Vblock:
  4. def __init__(self, data, pbh):
  5. self.data = data
  6. self.pbh = pbh
  7. self.vbh = self.calculate_vbh()
  8.  
  9. def calculate_vbh(self):
  10. return hashlib.sha256((self.data + self.pbh).encode()).hexdigest()
  11.  
  12. # 創建Vchain
  13. vblock1 = Vblock("Transaction 1 verified", "0")
  14. vblock2 = Vblock("Transaction 2 verified", vblock1.vbh)
  15. vblock3 = Vblock("Transaction 3 verified", vblock2.vbh)
  16.  
  17. # 驗證Vblock 3的完整性
  18. def verify_vblock(vblock, prev_vblock):
  19. if vblock.pbh == prev_vblock.vbh:
  20. print("Verification successful. Vblock", vblock.data, "is intact.")
  21. else:
  22. print("Verification failed. Vblock", vblock.data, "may have been tampered.")
  23.  
  24. verify_vblock(vblock3, vblock2)
Success #stdin #stdout 0.05s 11304KB
stdin
8
stdout
Verification successful. Vblock Transaction 3 verified is intact.