-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminihash.py
49 lines (41 loc) · 1.27 KB
/
minihash.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
import fnmatch
import hashlib
import hmac
import os
import sys
def hmac_sha1(filename, key=""):
seed = key.encode('utf-8')
sig = hmac.new(seed, None, hashlib.sha1)
for chunk in chunks(filename):
sig.update(chunk)
return sig.hexdigest()
def sha1(filename):
sig = hashlib.sha1()
for chunk in chunks(filename):
sig.update(chunk)
return sig.hexdigest()
def chunks(filename):
with open(filename, 'rb') as file:
while True:
chunk = file.read(1024)
if not chunk:
break
yield chunk
def find_files(directory, pattern):
for root, _, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
def main():
if len(sys.argv) < 2:
print("Usage: {0} root-dir".format(sys.argv[0]))
sys.exit(1)
for filepath in find_files(sys.argv[1], '*'):
try:
print("hmac-sha1: {0}, filepath: {1}".format(hmac_sha1(filepath), filepath))
except:
print("Could not generate hash for %s" % filepath)
# print("sha1: {0}, filepath: {1}".format(sha1(filepath), filepath))
if __name__ == '__main__':
main()