-
Notifications
You must be signed in to change notification settings - Fork 1
/
checksums.py
64 lines (49 loc) · 1.47 KB
/
checksums.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
import hashlib
import sys
import argparse
def argsParse(arguments):
"""checksums.py files -a md5
usage: checksums.py FILE [FILE ...] [-a ALGORITHM]
positional arguments:
FILE: file to hash
optional arguments:
ALGORITHM: md5, sha256, sha1 supported
"""
parser = argparse.ArgumentParser(description='checksums')
parser.add_argument('-a', '--algorithm', action='store',
default='md5', dest='algorithm', help='hash algorithm to be used' )
parser.add_argument('files', action='store', nargs='+',
help='files to be hashed')
return parser.parse_args(arguments)
def main():
args = argsParse(sys.argv[1:])
files, algorithm = args.files, args.algorithm
for fileTohash in files:
checksum = Checksums(fileTohash, algorithm)
#print(fileTohash + ":" + checksum.cal())
print(''.join([fileTohash, ':', checksum.cal()]))
class Checksums:
"""calculate md5, sha1, sha256..."""
def __init__(self, fileTohash, algorithm):
self.fileTohash = fileTohash
self.algorithm = algorithm
def getHashTool(self):
if self.algorithm in ('sha256', 'SHA256'):
return hashlib.sha256()
elif self.algorithm in ('md5', 'MD5'):
return hashlib.md5()
elif self.algorithm in ('sha1', 'SHA1'):
return hashlib.sha1()
else:
return hashlib.md5()
def cal(self):
tool = self.getHashTool()
with open(self.fileTohash) as f:
while True:
data = f.read(128)
if not data:
break
tool.update(data)
return tool.hexdigest()
if __name__ == "__main__":
main()