forked from pzread/judge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashDir.py
executable file
·38 lines (31 loc) · 968 Bytes
/
HashDir.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
#!/usr/bin/python3
'''Hash directory script.'''
import sys
import os
import hashlib
def main():
'''Main function.'''
pathlist = []
for root, dirs, files in os.walk(sys.argv[1], followlinks=False):
for name in dirs:
pathlist.append(os.path.join(root, name))
for name in files:
pathlist.append(os.path.join(root, name))
pathlist.sort()
allmd = hashlib.sha256()
for path in pathlist:
tmpmd = hashlib.sha256()
tmpmd.update(path.encode('utf-8'))
allmd.update(tmpmd.digest())
if os.path.isfile(path) and not os.path.islink(path):
regf = open(path, 'rb')
tmpmd = hashlib.sha256()
while True:
data = regf.read(65536)
if len(data) <= 0:
break
tmpmd.update(data)
allmd.update(tmpmd.digest())
print(allmd.hexdigest())
if __name__ == '__main__':
main()