-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
248 lines (184 loc) · 6.35 KB
/
util.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
#Importing necessary modules
import hashlib
from Crypto.PublicKey import RSA
from binascii import hexlify
from Crypto import Random
from Crypto.Hash import SHA256,SHA1,SHA224,SHA384,SHA512
from Crypto.Signature import PKCS1_v1_5
import random
def get_enc():
return SHA256.new()
#return SHA1.new()
#return SHA224.new()
#return SHA384.new()
#return SHA512.new()
def create_hash(s):
uni = s.encode()
#hash_object = hashlib.sha256(uni)
hash_object = get_enc()
hash_object.update(uni)
hex_dig = hash_object.hexdigest()
if get_debug():
print(hex_dig)
return(hex_dig)
def RSA_to_key(key):
return RSA.importKey(key)
def public_private_key():
rng = Random.new().read
#Generating private key (RsaKey object) of key length of 1024 bits
private_key = RSA.generate(1024, rng)
#Generating the public key (RsaKey object) from the private key
public_key = private_key.publickey()
if get_debug():
print("# Type of private key and public key")
print("# ",type(private_key), type(public_key))
'''It prints the following output.
<class ‘Crypto.PublicKey.RSA.RsaKey’>
<class‘Crypto.PublicKey.RSA.RsaKey’>
'''
'''
Now, the private_key is ‘RsaKey’ object. From it, we can create a
corresponding public key using the method ‘publickey()’ on the
‘RsaKey’ private_key object.
'''
#Converting the RsaKey (binary data) objects to string
#private_pem = private_key.export_key().decode()
private_pem = private_key.export_key()
#public_pem = public_key.export_key().decode()
public_pem = public_key.export_key()
if get_debug():
print("# Type of private pem and public pem")
print("# ",type(private_pem), type(public_pem))
print("# Private pem and public pem")
print("# ",private_pem,public_pem)
# return binary private and public key
return (private_pem, public_pem)
# to sign a message. here pri_key is in bytes
def sign_message(message,pri_key):
signer = PKCS1_v1_5.new(pri_key)
d = get_enc()
d.update(message.encode())
sig = signer.sign(d)
if get_debug():
print(sig.hex())
return sig
# to verify the signed message
def verify_message(message,sign,pub_key):
message_digest = get_enc()
message_digest.update(message.encode())
verifier = PKCS1_v1_5.new(pub_key)
verified = verifier.verify(message_digest, sign)
return verified
# creating a merkle tree
def create_merkle_tree(tot_leaves,leaf_sz):
leaves = tot_leaves
len_leaves = len(leaves)
if get_debug():
print("# total number of leaves :",len_leaves)
print("# leaves :",leaves)
#print("# total number of leaves :",len_leaves)
#print("# leaves :",leaves)
'''
Check if leaves are in order of leaf_sz if not then replicate the last
value of the leaves and make the number of leaves as the multiple of leaf_sz
'''
if len_leaves % leaf_sz != 0 :
# number of extra leaves to add to make it multiple of leaf_sz.
leaves_to_add = leaf_sz - (len_leaves % leaf_sz)
# final length of len_leaves will be.
len_leaves +=leaves_to_add
# adding the leaves.
for i in range(leaves_to_add):
leaves.append(leaves[-1])
if get_debug():
print("# New leaves :",leaves)
merkle_tree = [leaves]
while(len_leaves != 1):
temp = merkle_tree[-1]
cnt =0
pair_leaves = []
for i in range(len(temp)//leaf_sz):
temp1 = []
for j in range(leaf_sz):
temp1.append(temp[cnt])
cnt+=1
pair_leaves.append(temp1)
if get_debug():
print("# pair_leaves :",pair_leaves)
leaves = []
for i in range(len(pair_leaves)):
str_data = ""
for j in pair_leaves[i]:
str_data = str_data+str(j)
leaves.append(create_hash(str_data))
len_leaves = len(leaves)
if get_debug():
print("# total number of leaves :",len_leaves)
print("# leaves :",leaves)
if len_leaves % leaf_sz != 0 and len_leaves != 1:
# number of extra leaves to add to make it multiple of leaf_sz.
leaves_to_add = leaf_sz - (len_leaves % leaf_sz)
# final length of len_leaves will be.
len_leaves +=leaves_to_add
# adding the leaves.
for i in range(leaves_to_add):
leaves.append(leaves[-1])
merkle_tree.append(leaves)
return merkle_tree
#########################################################
######## Setting for the code ###########################
#########################################################
# Initial amount for all the nodes
def get_initial_amount():
return 5000
# Block creation reward
def get_block_create_reward():
return 5
# Block creation time
def get_block_create_time():
return 20
# Transaction charges
def get_transaction_charges():
return 2
# For smart contract
def get_smart_contract():
return True
def get_smart_contract_nodes():
dict_nodes = {}
dict_nodes[0] = 1
return dict_nodes
def get_smart_contract_balance():
return 3000
def get_smart_contract_deduction():
return 50
def get_multi_smart_contract():
return True
def get_multi_transactions():
return False
def get_random_multi_transactions():
return random.randint(1,4)
def get_random_amount():
return random.randint(1, 100)
def find_random_reciever(tot_nodes,curr_node):
r = random.randint(0,tot_nodes-1)
while (r == curr_node):
r=r+1
r= r%tot_nodes
return r
# Max time for which each node will run
def get_total_time():
return 500
# for debuging and printing logs of the program
def print_logs():
return True
def get_debug():
return False
'''
if _debug_:
# here the keys are in binary
private_k,public_k=public_private_key()
k = (sign_message("hello world",RSA_to_key(private_k)))
print(verify_message("hello world",k,RSA_to_key(public_k)))
print(create_merkle_tree(['1','1','2','3','5','6','7','9','10','11','13','15','19'],4))
'''