-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashapp.py
91 lines (73 loc) · 2.77 KB
/
hashapp.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from pathlib import Path
import click, hashlib
__VERSION__ = "1.0.0"
# Gets the content and hash function from get_hash() and returns to it hash retrieved with the selected hash function
def getHash(content, hash_func):
hash = None
if hash_func == "md5":
hash = hashlib.md5(content).hexdigest()
return hash
elif hash_func == "sha1":
hash = hashlib.sha1(content).hexdigest()
return hash
elif hash_func == "sha224":
hash = hashlib.sha224(content).hexdigest()
return hash
elif hash_func == "sha256":
hash = hashlib.sha256(content).hexdigest()
return hash
elif hash_func == "sha384":
hash = hashlib.sha384(content).hexdigest()
return hash
else:
click.secho(f"[ ERROR ] Unknown hash function: '{hash_func}'", fg="red")
# This function checks existence of a file. If exists it returns True otherwise returns False
def check_file_existence(file):
if Path(file).exists():
return True
else:
return False
@click.group()
def hashapp():
"""An utitlity written in python that can check and verify hash of a file"""
pass
# This function retrives hash of the user specified file and prints the hash found with the selected hash function
@click.command(name="get-hash", help="Gets the hash of a file")
@click.option("--file", "-f", help="Filename or path to the file")
@click.option("--hash_func", "-hf", default="sha256", help="Hash function")
def get_hash(file, hash_func):
if check_file_existence(file):
file = open(file, 'rb')
buff = file.read()
hash = getHash(buff, hash_func)
click.secho(hash, fg="blue")
else:
click.secho(f"[ ERROR ] No such file: '{file}'", fg="red")
"""
This function verifies the user specified file with the user specified hash and prints '0' if the file is OK otherwise
prints '1'
"""
@click.command(name="verify", help="Verifies the hash of a file")
@click.option("--file", "-f", help="Filename or path to the file")
@click.option("--hash_func", "-hf", default="sha256", help="Hash function")
@click.option("--hash", "-h")
def verify(file, hash_func, hash):
if check_file_existence(file):
file = open(file, 'rb')
buff = file.read()
file_hash = getHash(buff, hash_func)
if hash == file_hash:
click.secho(0, fg="green")
else:
click.secho(1, fg="red")
else:
click.secho(f"[ ERROR ] No such file: '{file}'", fg="red")
# This function prints out the installed HashApp version
@click.command(name="version", help="Prints the installed HashApp version")
def version():
click.echo(f"HashApp {__VERSION__}")
hashapp.add_command(get_hash)
hashapp.add_command(verify)
hashapp.add_command(version)
if __name__ == "__main__":
hashapp()