Advertisement
BilakshanP

Simple Text Data Block Chain Implementation

Feb 26th, 2024 (edited)
985
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | Cybersecurity | 0 0
  1. import hashlib
  2. import datetime
  3.  
  4. class Block:
  5.     def __init__(self, index: int, timestamp: datetime.datetime, data: str, previous_hash: str):
  6.         self.index = index
  7.         self.timestamp = timestamp
  8.         self.data = data
  9.         self.previous_hash = previous_hash
  10.         self.hash = self.calculate_hash()
  11.  
  12.     def calculate_hash(self):
  13.         sha = hashlib.sha256()
  14.         sha.update((str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)).encode('utf-8'))
  15.         return sha.hexdigest()
  16.  
  17. class Blockchain:
  18.     def __init__(self):
  19.         self.chain = [self.create_genesis_block()]
  20.  
  21.     def create_genesis_block(self) -> Block:
  22.         return Block(0, datetime.datetime.now(), "Genesis Block", "0")
  23.  
  24.     def get_latest_block(self) -> Block:
  25.         return self.chain[-1]
  26.  
  27.     def add_block(self, new_block: Block):
  28.         new_block.previous_hash = self.get_latest_block().hash
  29.         new_block.hash = new_block.calculate_hash()
  30.         self.chain.append(new_block)
  31.  
  32.     def is_chain_valid(self) -> bool:
  33.         for i in range(1, len(self.chain)):
  34.             current_block = self.chain[i]
  35.             previous_block = self.chain[i - 1]
  36.             if current_block.hash != current_block.calculate_hash():
  37.                 return False
  38.             if current_block.previous_hash != previous_block.hash:
  39.                 return False
  40.         return True
  41.    
  42.     def print_blockchain(self):
  43.         for block in blockchain.chain:
  44.             print("Index:", block.index)
  45.             print("Timestamp:", block.timestamp)
  46.             print("Data:", block.data)
  47.             print("Previous Hash:", block.previous_hash)
  48.             print("Current Hash:", block.hash)
  49.             print()
  50.  
  51.  
  52. # Example usage
  53. blockchain = Blockchain()
  54. blockchain.add_block(Block(1, datetime.datetime.now(), "Data 1", ""))
  55. blockchain.add_block(Block(2, datetime.datetime.now(), "Data 2", ""))
  56. blockchain.add_block(Block(3, datetime.datetime.now(), "Data 3", ""))
  57. blockchain.add_block(Block(4, datetime.datetime.now(), "Data 4", ""))
  58.  
  59. # Print blockchain
  60. blockchain.print_blockchain()
  61.  
  62. print("Blockchain is valid:", blockchain.is_chain_valid())
  63.  
  64. # Modify data to demonstrate tampering
  65. blockchain.chain[2].data = "Tampered Data"
  66. print("Blockchain is valid after tampering:", blockchain.is_chain_valid())
  67.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement