-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchainn.py
More file actions
80 lines (60 loc) · 2.46 KB
/
Copy pathBlockchainn.py
File metadata and controls
80 lines (60 loc) · 2.46 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import hashlib
import datetime
import base64
# from pyCrypto import Random
# from Crypto.Cipher import AES
# BS = 16
# pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
# unpad = lambda s: s[0:-ord(s[-1])]
# class AESAlgorithm:
# def __init__( self):
# pass
# def encrypt( self, message, symmetricKey):
# raw = pad(message)
# symmetricKey = hashlib.sha256(symmetricKey.encode('utf-8')).digest()
# iv = Random.new().read( AES.block_size)
# cipher = AES.new( symmetricKey, AES.Mode_CBC, iv)
# return base64.b64encode(iv + cipher.encrypt( raw ))
# def decrypt( self, enc, symmetricKey):
# enc = base64.b64secode(enc)
# symmetricKey = hashlib.sha256(symmetricKey.encode('utf-8')).digest()
# iv = enc[:16]
# cipher = AES.new(symmetricKey, AES.MODE_CBC, iv)
# return unpad(cipher.decrypt( enc[16:] ))
# AESAlgorithm = AESAlgorithm()
# plaintext="hello guys"
# symKey1 = "1234"
# encryptedText = AESAlgorithm.encrypt(plaintext, symKey1)
# print(encryptedText)
##First Class
class block:
##block format
def __init__(self, previous_block_hash, timeStamp, data):
self.previous_block_hash = previous_block_hash
self.timestamp = timeStamp
self.data = data
self.chain = []
# self.generate_blocks()
## Appending all data in block_data
self.block_data = "-" .join(data) + "-" + timeStamp + "-" + previous_block_hash
## Hashing the block
self.block_hash = hashlib.sha256(self.block_data.encode()).hexdigest()
def generate_blocks(self):
new_block= block(data=input('data: '), timeStamp=str(datetime.datetime.now()), previous_block_hash="12345")
self.chain.append(new_block)
return self.chain
##Testing for block one, no previous hash
t1 = "first"
t2 = "second"
t3 = "3rd"
data="123456789"
timestamp=str(datetime.datetime.now())
# timestamp= datetime.date.today().strftime("%H:%M:%S")
initial_block = block(data, timestamp, [t1, t1])
print(initial_block.block_data)
print(initial_block.block_hash)
##Hashing is working, if something is changed in the input, then the hashing will change.
##Testing for any other block, hashing of the previous block exists
secondBlock = block(timestamp, initial_block.block_hash, data)
print(secondBlock.block_data)
print(secondBlock.block_hash)