-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashgen.py
38 lines (32 loc) · 1.05 KB
/
hashgen.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
'''
' Python program to generate <MD5/SHA1/SHA256/SHA512> hashes of a file using hashlib module
' @author: Sanjan Geet Singh
'''
from hashlib import md5, sha1, sha256, sha512 # hashlib module
from sys import argv
if len(argv) != 3: # Insufficient number of arguments supplied
print("Usage: python \"Hash Generator.py\" [Algorithm] [File to Hash]")
print("Algorithms Avaliable: md5, sha1, sha256, sha512")
exit()
algorithm = argv[1].lower()
file_to_hash = argv[2]
try:
file = open(file_to_hash, 'rb')
contents = file.read()
file.close()
except FileNotFoundError:
print("[ERROR] File Not Found")
exit()
if algorithm == 'md5':
ghash = md5(contents)
elif algorithm == 'sha1':
ghash = sha1(contents)
elif algorithm == 'sha256':
ghash = sha256(contents)
elif algorithm == 'sha512':
ghash = sha512(contents)
else:
print("[ERROR] Unknown Hashing Algorithm")
exit()
ghash = ghash.hexdigest() # convert generated hash to hex form
print(ghash)