-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathhash_the_file.py
More file actions
71 lines (58 loc) · 1.42 KB
/
hash_the_file.py
File metadata and controls
71 lines (58 loc) · 1.42 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
68
69
70
71
import sys
import hashlib
# Some functions may not be available in some
# systems and in python2
HASH_MAP = {
"--md5": hashlib.md5,
"--sha1": hashlib.sha1,
"--sha224": hashlib.sha224,
"--sha256": hashlib.sha256,
"--sha384": hashlib.sha384,
"--sha512": hashlib.sha512,
"--blake2b": hashlib.blake2b,
"--blake2s": hashlib.blake2s,
"--sha3_224": hashlib.sha3_224,
"--sha3_256": hashlib.sha3_256,
"--sha3_384": hashlib.sha384,
"--sha3_512": hashlib.sha512,
"--shake_128": hashlib.shake_128,
"--shake_256": hashlib.shake_256,
}
def help():
print("Usage: python main.py [--<method>] filenames")
print("Available hash methods")
for method in HASH_MAP:
print(" ", method)
if len(sys.argv) < 2:
help()
sys.exit(0)
offset = 1
flag = False
potential_flag = sys.argv[1]
if potential_flag[:2] == "--":
offset = 2
flag = potential_flag
if flag:
if flag == "--help":
help()
exit(0)
try:
hsh = HASH_MAP[flag]
except KeyError:
print("Invalid flag", flag)
sys.exit(1)
except:
raise
else:
print("No method specified. Using md5.")
hsh = hashlib.md5
filenames = sys.argv[offset:]
if len(filenames) == 0:
help()
sys.exit(1)
for filename in filenames:
fd = open(filename, "r")
content = fd.read()
m = hsh(str.encode(content))
print(m.hexdigest() + " " + filename)
fd.close()