-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashMyFiles.py
108 lines (86 loc) · 3.36 KB
/
hashMyFiles.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#Python2.7
import os
import hashlib
import csv
import sys
import argparse
def hash_file(file):
"""Calculates file hashes utilizing md5, sha1, & sha256 algorithms
:param file: Full path of file to be hashed
:type file: str
:returns: dictionary containing hash type/value pairs
:rtype: dict
"""
hasherOptions = [hashlib.md5(), hashlib.sha1(), hashlib.sha256()]
hashResults = {}
for hash in hasherOptions:
with open(file, 'rb') as afile:
buf = afile.read(65536)
while len(buf) > 0:
hash.update(buf)
buf = afile.read(65536)
hashResults[str(hash).split()[0][1:]] = hash.hexdigest()
return hashResults
def walk_dir(directory):
"""Recursively walks directory returning a file listing
:param directory: Full path of file to be hashed
:type directory: str
:returns: full paths for each file under directory
:rtype: list
"""
count = 0
walkList = []
print "Walking directory: " + directory
for dirName, subdirList, fileList in os.walk(directory):
for fname in fileList:
walkList.append(os.path.join(dirName, fname))
count += 1
print " Complete: " + str(count) + " files identified"
return walkList
def progress_bar(iteration, total, prefix='', suffix='', decimals=2, barLength=100):
"""Displays a progress bar on screen while called in a loop
:param iterations: Current iteration - Required
:param total: Total iterations - Required
:param prefix: Prefix to progress bar
:param suffix: Suffix to progress bar
:type iteration: int
:type total: int
:type prefix: str
:type suffix: str
:returns: n/a
:rtype: n/a
:Example:
testList=[A,B,C,D]
i=1
for i in len(testList):
print i
progress_bar(i, len(testList), prefix='Print Progress', suffix='Complete', decimals=2, barLength=100)
i += 1
"""
filledLength = int(round(barLength * iteration / float(total)))
percents = round(100.00 * (iteration / float(total)), decimals)
bar = '#' * filledLength + '-' * (barLength - filledLength)
sys.stdout.write('\r%s [%s] %s%s %s' % (prefix, bar, percents, '%', suffix)),
sys.stdout.flush()
if iteration == total:
print("\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Directory crawler & hahser')
parser.add_argument('-i', dest='input', help='Directory to be crawled and hashed')
parser.add_argument('-o', dest='output', help='Output file (CSV)')
args = parser.parse_args()
outFile = open(args.output, 'wb')
outCSV = csv.writer(outFile)
outCSV.writerow(['File', 'MD5', 'SHA1', 'SHA256'])
hashingResults = {}
fileListing = walk_dir(os.path.normpath(args.input))
print 'Hashing Files'
listLength = len(fileListing)
i = 1
for entry in fileListing:
progress_bar(i, listLength, prefix = ' Hashing', suffix = 'Complete')
hashingResults[entry] = hash_file(entry)
outCSV.writerow(
[entry, hashingResults[entry]['md5'], hashingResults[entry]['sha1'], hashingResults[entry]['sha256']])
i += 1
raw_input('Complete, press any key to exit')