-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHasher.py
More file actions
38 lines (31 loc) · 1.11 KB
/
FileHasher.py
File metadata and controls
38 lines (31 loc) · 1.11 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
import hashlib
def compute_hashes(file_path) -> dict | None:
sha512_hash = hashlib.sha512()
sha256_hash = hashlib.sha256()
md5_hash = hashlib.md5()
if file_path == "":
print(f"No file entered. Please specify a file name.\n")
return None
try:
with open(file_path, "rb") as f:
for byte_block in iter(lambda: f.read(4096), b""):
sha512_hash.update(byte_block)
sha256_hash.update(byte_block)
md5_hash.update(byte_block)
except FileNotFoundError as e:
print(f"Error: {e}")
return None
return {
'SHA-512': sha512_hash.hexdigest(),
'SHA-256': sha256_hash.hexdigest(),
'MD5': md5_hash.hexdigest()
}
while (True):
file_path = input("Enter a file to hash: ")
hash_results = compute_hashes(file_path)
if hash_results is None:
continue
print(f"Hashes for '{file_path}':\n")
print(f"SHA-512: {hash_results['SHA-512']}\n")
print(f"SHA-256: {hash_results['SHA-256']}\n")
print(f"MD5: {hash_results['MD5']}\n")