-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhashlist.py
56 lines (48 loc) · 1.56 KB
/
hashlist.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
#!/usr/bin/env python
##
# directory tree containing file names and hashes
# github.com/deadbits
##
import sys
import os
import hashlib
def is_valid(file_path):
if os.path.exists(file_path):
if os.path.isfile(file_path):
if os.path.getsize(file_path) > 0:
return True
return False
def get_hash(f, hash_type):
fin = open(f, 'rb')
if hash_type == 'md5':
m = hashlib.md5()
elif hash_type == 'sha1':
m = hashlib.sha1()
while True:
data = fin.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
if __name__ == '__main__':
try:
root_dir = sys.argv[1]
if not os.path.exists(root_dir):
print 'error: path %s does not exist' % root_dir
sys.exit(1)
if os.path.isfile(root_dir):
print 'error: %s is not a directory' % root_dir
sys.exit(1)
except IndexError:
print 'usage: ./hashdeep.py <root directory>'
sys.exit(0)
# http://stackoverflow.com/questions/9727673/list-directory-tree-structure-using-python
for root, dirs, files in os.walk(root_dir):
level = root.replace(root_dir, '').count(os.sep)
indent = ' ' * 4 * (level)
print '%s%s/' % (indent, os.path.basename(root))
subindent = ' ' * 4 * (level + 1)
for fpath in [os.path.join(root, f) for f in files]:
md5 = get_hash(fpath, 'md5')
name = os.path.relpath(fpath, root_dir)
print '%s%s\n%sMD5: %s\n' % (subindent, name, subindent, md5)