-
Notifications
You must be signed in to change notification settings - Fork 3
/
LadderElements.py
50 lines (35 loc) · 1.28 KB
/
LadderElements.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
#
import hashlib
def SimpleMerkleRoot(hashes, hash_function=hashlib.sha256):
"""
Return the "Simple" Merkle Root Hash as a byte blob from an iterable
ordered list of byte blobs containing the leaf node hashes.
Works by recursively hashing together pairs of consecutive hashes to
form a reduced set one level up from the leafs, repeat until we are
reduced to a single hash. If list is odd-length, then last element is
copied, not hashed.
"""
def BytesHasher(msgbytes):
return hash_function(msgbytes).digest()
if len(hashes) == 0:
hashes = [ BytesHasher(bytes()) ] # Hash of empty data
#line = ""
#for h in hashes:
# line = line + h.hex() + " "
#print(line)
if len(hashes) == 1:
return hashes[0]
reduced = []
ilast = len(hashes) - 1
for i in range(len(hashes))[0::2]: # 0, 2, 4, 6, ...
if i < ilast:
pre = hashes[i] + hashes[i+1]
reduced.append( BytesHasher(pre) )
else:
reduced.append(hashes[i])
return SimpleMerkleRoot(reduced, hash_function)
if __name__ == "__main__":
print ("Aye!")
pretexts = []#"A","B","C"]#,"D"]
hashes = [hashlib.sha256(bytes(s,'utf8')).digest() for s in pretexts]
root = SimpleMerkleRoot(hashes)