-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmt.py
65 lines (58 loc) · 1.74 KB
/
mt.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
import os
import hashlib
def get_local_data(path):
paths = os.listdir(path)
data = []
for p in paths:
fp = open(path+'/'+p)
data.append(fp.read())
return data
#print(get_local_data('./doc'))
def add_hash(arr):
#只处理一个节点的或两个节点的
hash = hashlib.md5()
if len(arr) == 1:
hash.update(bytes(arr[0], encoding='utf-8'))
return hash.hexdigest()
elif len(arr) == 2:
hash.update(bytes(arr[0], encoding='utf-8'))
hash.update(bytes(arr[1], encoding='utf-8'))
return hash.hexdigest()
else:
print('数据错误...')
return ''
def data_to_hash(arr):
result = []
for i in arr:
hash = hashlib.md5()
hash.update(bytes(str(i), encoding='utf-8'))
result.append(hash.hexdigest())
return result
def markle(arr):
print(arr)
arr.reverse()
if len(arr) == 1:
return arr
tmp =[]
result = []
index = -1
while(len(arr) > 0):
index = index + 1
item = arr.pop()
if index % 2 == 0:
tmp.append(item)
if len(arr) == 0:
result.append(add_hash(tmp))
tmp = []
else:
tmp.append(item)
result.append(add_hash(tmp))
tmp = []
markle(result)
def main():
#data = [11,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
data = get_local_data('./doc')
hashdata = data_to_hash(data)
markle(hashdata)
if __name__ == '__main__':
main()