Skip to content

BLOCKCHAIN #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Rohitkadlag opened this issue Aug 20, 2022 · 0 comments
Closed

BLOCKCHAIN #168

Rohitkadlag opened this issue Aug 20, 2022 · 0 comments

Comments

@Rohitkadlag
Copy link

import hashlib

def hashGenerator(data):
result=hashlib.sha256(data.encode())
return result.hexdigest()

class Block:
def init(self,data,hash,prev_hash):
self.data=data
self.hash=hash
self.prev_hash=prev_hash

class Blockchain:
def init(self):
hashLast=hashGenerator('gen_last')
hashStart=hashGenerator('gen_hash')

  genesis=Block('gen-data',hashStart,hashLast)
  self.chain=[genesis]

def add_block(self,data):
    prev_hash=self.chain[-1].hash
    hash=hashGenerator(data+prev_hash)
    block=Block(data,hash,prev_hash)
    self.chain.append(block)

bc=Blockchain()
bc.add_block('1')
bc.add_block('2')
bc.add_block('3')

for block in bc.chain:
print(block.dict)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant