-
Notifications
You must be signed in to change notification settings - Fork 1
/
cryptopython.py
53 lines (36 loc) · 1.38 KB
/
cryptopython.py
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
import hashlib
from datetime import datetime, date
class Block:
def __init__(self,data,previoushash = ''):
self.data = data
self.timestanp = datetime.now()
self.previoushash = previoushash
self.hash = self.addHash()
def addHath(self):
block_hash = hashlib.sha256()
data = '_'.join(map(str,self.data))
block_hash.update(
data +
self.timestanp.isoformat() + self.previoushash
)
return block_hash.hexdigest()
class Blockchain:
def __init__(self):
self.chain = [self.genesisBlock()]
def genesisBlock(self):
return Block([],'000')
def lastChain(self):
return self.chain[-1]
def addBlock(self, newblock):
newblock.previoushash = self.lastChain().hash
newblock.hash =newblock.addHash()
self.chain.append(newblock)
def chainvaild(self):
for i in range(1,len(self.chain)):
block =self.chain[i]
previous_block = self.chain[i-1]
if block.hash != block.addHath():
return False
elif block.previoushash != previous_block.hash:
return False
return True