-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerkle_tree.py
84 lines (63 loc) · 2.3 KB
/
merkle_tree.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
#######################################
# PART 1 #
#######################################
from hashlib import sha256
import time
start_time = time.time()
def h(s): return sha256(s.encode()).hexdigest() # hashing helper function
def base(sentence: str) -> list:
'''Hash the base layer, padding excluded'''
words = sentence.split() # creates list of words in the sentence
hashed = []
# hashes the elements and adds it to the list
for word in words:
hashed.append(h(word))
#print(hashed)
return hashed
def tree(root: list) -> str:
'''merkliezes the hashed base list with padding, down to the root'''
while len(root) != 1: # when length == 1, it is the root
branch = []
# i increments over 2 values, therefore hashing the two adjacent elements
for i in range(0, len(root), 2):
branch.append(h(root[i] + root[i+1])) # hashes and adds it to the list
root = branch
#print(root)
# returns the root as a string
return root[0]
def merklieze(sentence: str) -> str:
'''Combines all the functions and adds the padding'''
hash = base(sentence) # hash of base layer
length = len(hash)
if length == 1: # 1 was a weird corner case, thus explicit add
return hash[0]
else: # adds padding ('\x00') if not power of 2
p = 1
for i in range(length):
if p < length:
p *= 2
else:
n = (2**i) - length
if n == 0: break # if power of 2
else: # if not power of 2
for j in range(n):
hash.append(chr(0))
break
# passes the list to function tree() and returns the value recieved
return tree(hash)
'''
with open('merkle_instructions.txt', 'r') as file:
st = file.read()
'''
print(merklieze('Lavana Loves Browny!!!!'))
'''
print(merklieze('a b c d'))
string1 = """
ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc618ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4
"""
#print(merklieze(string1))
#print(merklieze('62af5c3cb8da3e4f25061e829ebeea5c7513c54949115b1acc225930a90154dad3a0f1c792ccf7f1708d5422696263e35755a86917ea76ef9242bd4a8cf4891a'))
print(tree(['96e024ba2074fe77e8e965ba43a704be', 'c0a594722e66ed5e4dec538fdc1fc1ba']))
'''
print("--- %s seconds ---" % (time.time() - start_time))