-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashing.py
More file actions
55 lines (47 loc) · 1.3 KB
/
hashing.py
File metadata and controls
55 lines (47 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import hashlib
"""
s="HAHAHA"
digest= hashlib.sha256(s.encode()).hexdigest();
print(digest);
<>
"""
lastNode=""
block0={"name":"", "age":"", "previousHash":hashlib.sha256("000".encode()).hexdigest(), "blockHash":""}
block0['blockHash']= hashlib.sha256(str(block0).encode()).hexdigest()
blockchain= [block0]
def createBlock():
age= int(input("age: "))
name= input("name: ")
block={'age':age, 'name':name}
print("\nBlock successfully created!")
print(block)
return block
def addBlock(block):
if(block['age'] > 50 or len(block['name']) > 5):
print("Invalid Block\nRejected!")
return
block["previousHash"]= blockchain[-1]["blockHash"]
block["blockHash"]= hashlib.sha256( str(block).encode() ).hexdigest()
blockchain.append(block)
print("\nNew block added!")
print("hash:")
print(block["blockHash"])
print("\n B L O C K C H A I N:")
print(blockchain)
"""
def verify():
print("\n Verification:\n ")
age= int(input("age: "))
name= input("name: ")
block={'age':age, 'name':name}
blockHash= hashlib.sha256( str(block).encode() ).hexdigest()
if(blockchain[blockHash] == block):
print("OK")
else:
print("ERROR")
"""
while 1:
addBlock(createBlock())
if(input("Continue ? ") != "y"):
break
verify()