forked from sakura-editor/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc-hash.py
56 lines (49 loc) · 1.43 KB
/
calc-hash.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
import hashlib
import os
import sys
def getSha256(fileName):
hash = hashlib.sha256()
with open(fileName, 'rb') as f:
while True:
chunk = f.read(hash.block_size)
if chunk:
hash.update(chunk)
else:
break
checksum = hash.hexdigest()
return checksum
def writeHash(hashFile, topdir, extensions):
def isMatch(fileName, extensions):
base, ext = os.path.splitext(fileName)
if extensions:
for extension in extensions:
if ext == extension or ext == "." + extension:
return True
return False
else:
return True
with open(hashFile, "w") as fout:
checksums = {}
for root, dirs, files in os.walk(topdir):
for fileName in files:
full_path = os.path.join(root, fileName)
if isMatch(fileName, extensions):
checksum = getSha256(full_path)
if checksum not in checksums:
checksums[checksum] = []
checksums[checksum].append(full_path)
for key, valueList in checksums.items():
for value in valueList:
fout.write(key + "\t" + value + "\n")
print (key + "\t" + value)
if __name__ == '__main__':
if len(sys.argv) < 3:
print ("usage: " + sys.argv[0] + " <output hash file name> <search directory>")
print ("usage: " + sys.argv[0] + " <output hash file name> <search directory> [extension1] [extension2]")
sys.exit(1)
hashFile = sys.argv[1]
topdir = sys.argv[2]
extensions = []
if len(sys.argv) > 3:
extensions = sys.argv[3:]
writeHash(hashFile, topdir, extensions)