-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-hash.py
More file actions
38 lines (30 loc) · 1.31 KB
/
generate-hash.py
File metadata and controls
38 lines (30 loc) · 1.31 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 argparse
import hashlib
def calculate_hash(text, algorithm):
"""Calculate the specified hash of the given text."""
try:
# Create a hash object using the specified algorithm
hash_obj = hashlib.new(algorithm)
except ValueError as e:
raise ValueError(f"Unsupported hash algorithm: {algorithm}") from e
# Update the hash object with the byte-encoded text
hash_obj.update(text.encode('utf-8'))
# Return the hexadecimal digest of the hash
return hash_obj.hexdigest()
def main():
# Create argument parser
parser = argparse.ArgumentParser(description='Calculate the hash value of the given text.')
# Add command-line arguments
parser.add_argument('text', type=str, help='Text to hash')
parser.add_argument('algorithm', type=str, choices=hashlib.algorithms_available,
help='Hash algorithm to use. Available choices: ' + ', '.join(hashlib.algorithms_available))
# Parse command-line arguments
args = parser.parse_args()
# Get the text and algorithm from arguments
text = args.text
algorithm = args.algorithm
# Calculate the hash and print the result
hash_value = calculate_hash(text, algorithm)
print(f'The {algorithm} hash of the text is: {hash_value}')
if __name__ == '__main__':
main()