-
Notifications
You must be signed in to change notification settings - Fork 1
/
sha384sum.py
50 lines (40 loc) · 1.29 KB
/
sha384sum.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
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 22-11-2024
Last update: 22-11-2024
Version: 1.0
Description: The sha384sum command from GNU Coreutils in Python3
Example of use: python3 sha384sum.py file.txt
'''
import sys
import hashlib
def compute_sha384sum(filename):
"""
Compute SHA-384 hash of a file, identical to sha384sum command.
Args:
filename (str): Path to the file to hash
Returns:
str: SHA-384 hash in hexadecimal format
"""
sha384_hash = hashlib.sha384()
try:
with open(filename, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
sha384_hash.update(chunk)
return f"{sha384_hash.hexdigest()} {filename}"
except FileNotFoundError:
print(f"sha384sum: {filename}: No such file or directory", file=sys.stderr)
sys.exit(1)
except PermissionError:
print(f"sha384sum: {filename}: Permission denied", file=sys.stderr)
sys.exit(1)
def main():
if len(sys.argv) < 2:
print("Usage: ./sha384sum.py <file1> [file2 ...]", file=sys.stderr)
sys.exit(1)
for filename in sys.argv[1:]:
print(compute_sha384sum(filename))
if __name__ == "__main__":
main()