This repository was archived by the owner on Apr 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.py
More file actions
67 lines (44 loc) · 1.73 KB
/
sum.py
File metadata and controls
67 lines (44 loc) · 1.73 KB
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
import argparse
import binascii
import hashlib
class Sum:
def main():
args = argparse.ArgumentParser()
args.add_argument("-e", "--export", help = "File to hash", required = False)
args.add_argument("-a", "--algo", help = "Algorithm to use", required = False)
args.add_argument("-f", "--file", help = "Hash file exported", required = True)
args.add_argument("-v", "--verify", help = "Compare two hashes", required = False)
argument = args.parse_args()
content = open(argument.file, 'rb').read()
if argument.algo == "md5":
result = hashlib.md5(content).hexdigest()
elif argument.algo == "crc32":
result = str("%08X" % binascii.crc32(content)).lower()
elif argument.algo == "sha1":
result = hashlib.sha1(content).hexdigest()
elif argument.algo == "sha224":
result = hashlib.sha224(content).hexdigest()
elif argument.algo == "sha256":
result = hashlib.sha256(content).hexdigest()
elif argument.algo == "sha384":
result = hashlib.sha384(content).hexdigest()
elif argument.algo == "sha512":
result = hashlib.sha512(content).hexdigest()
elif argument.algo == "blake2b":
result = hashlib.blake2b(content).hexdigest()
else:
result = hashlib.md5(content).hexdigest()
if (argument.export):
hash_file = open(argument.export + "." + argument.algo, 'w')
hash_file.write("*" + argument.file + "* " + result)
print("Hash exported to " + argument.export + "." + argument.algo)
if (argument.verify):
if (result == argument.verify):
print("Hash is correct")
print(str(result) + " == " + argument.verify)
else:
print("Hash is incorrect")
print(str(result) + " != " + argument.verify)
else:
print(result)
Sum.main()