-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.py
More file actions
57 lines (46 loc) · 1.39 KB
/
Copy pathhash.py
File metadata and controls
57 lines (46 loc) · 1.39 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import hashlib
def checkValid(filepath, type):
if not (os.path.exists(filepath)):
return [False, 'File is not existed!(%s)' %filepath]
try:
nameSpace=__import__('hashlib')
except:
return [False, 'There is no module of hashlib on your system.']
try:
func = getattr(nameSpace, type)
sign = func()
return [True, "Suc"]
except:
return [False, "Cannot find " + type]
def traversal(output, filepath, type):
if (os.path.isfile(filepath)):
saveHashToFile(output, filepath)
if (os.path.isdir(filepath)):
for filename in os.listdir(filepath):
traversal(output, os.path.join(filepath, filename), type)
def saveHashToFile(output, filename):
file = open(filename, 'rb')
sign = hashlib.md5()
while True:
data = file.read(4096)
if not data:
break;
sign.update(data)
output.writelines(file.name.split('/')[-1] + " >>> " + sign.hexdigest() + '\r\n')
file.close()
if __name__=='__main__':
filepath = sys.argv[1]
try:
hash=sys.argv[2]
except:
hash='md5'
isValid = checkValid(filepath, hash)
if (isValid[0]):
with open('/home/chrischeng/TestHash.txt', 'w') as output:
traversal(output, filepath, hash)
else :
print(result[1])