-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.py
More file actions
142 lines (114 loc) · 5.37 KB
/
Copy pathhash.py
File metadata and controls
142 lines (114 loc) · 5.37 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import hashlib
choice = input("1. Hash a file\n2. Enter Text to hash\n3. check file integrity\n4. exit\nEnter your choice: ")
if choice == "1":
hasher = input("Choose the hash algorithm (SHA256, SHA512, SHA1, md5): ")
if hasher == "SHA256":
try:
file_path = input("Enter the file path: ")
print(f"using {hasher} algorithm")
with open(file_path, 'rb') as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
print("hash value is :", file_hash)
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
elif hasher == "SHA512":
try:
file_path = input("Enter the file path: ")
print(f"using {hasher} algorithm")
with open(file_path, 'rb') as f:
file_hash = hashlib.sha512(f.read()).hexdigest()
print("hash value is :", file_hash)
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
elif hasher == "SHA1":
try:
file_path = input("Enter the file name: ")
print(f"using {hasher} algorithm")
with open(file_path, 'rb') as f:
file_hash = hashlib.sha1(f.read()).hexdigest()
print("hash value is :", file_hash)
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
elif hasher == "md5":
try:
file_path = input("Enter the file name: ")
print(f"using {hasher} algorithm")
with open(file_path, 'rb') as f:
file_hash = hashlib.md5(f.read()).hexdigest()
print("hash value is :", file_hash)
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
else:
print("Invalid hash algorithm. Please choose either SHA256, SHA512, SHA1 or md5.")
if choice == "2":
hasher = input("Choose the hash algorithm (SHA256, SHA512, SHA1, md5): ")
if hasher == "SHA256":
text = input("Enter the text to hash: ")
print(f"using {hasher} algorithm")
text_hash = hashlib.sha256(text.encode()).hexdigest()
print("hash value is :", text_hash)
elif hasher == "SHA512":
text = input("Enter the text to hash: ")
print(f"using {hasher} algorithm")
text_hash = hashlib.sha512(text.encode()).hexdigest()
print("hash value is :", text_hash)
elif hasher == "SHA1":
text = input("Enter the text to hash: ")
print(f"using {hasher} algorithm")
text_hash = hashlib.sha1(text.encode()).hexdigest()
print("hash value is :", text_hash)
elif hasher == "md5":
text = input("Enter the text to hash: ")
print(f"using {hasher} algorithm")
text_hash = hashlib.md5(text.encode()).hexdigest()
print("hash value is :", text_hash)
else:
print("Invalid hash algorithm. Please choose either SHA256, SHA512, SHA1 or md5.")
if choice == "3":
file_path = input("Enter the file name: ")
expected_hash = input("Enter the expected hash value: ")
hasher = input("Choose the hash algorithm (SHA256, SHA512, SHA1, md5): ")
if hasher == "SHA256":
try:
with open(file_path, 'rb') as f:
file_hash = hashlib.sha256(f.read()).hexdigest()
if file_hash == expected_hash:
print("File integrity verified. The hash values match.")
else:
print("File integrity verification failed. The hash values do not match.")
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
elif hasher == "SHA512":
try:
with open(file_path, 'rb') as f:
file_hash = hashlib.sha512(f.read()).hexdigest()
if file_hash == expected_hash:
print("File integrity verified. The hash values match.")
else:
print("File integrity verification failed. The hash values do not match.")
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
elif hasher == "SHA1":
try:
with open(file_path, 'rb') as f:
file_hash = hashlib.sha1(f.read()).hexdigest()
if file_hash == expected_hash:
print("File integrity verified. The hash values match.")
else:
print("File integrity verification failed. The hash values do not match.")
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
elif hasher == "md5":
try:
with open(file_path, 'rb') as f:
file_hash = hashlib.md5(f.read()).hexdigest()
if file_hash == expected_hash:
print("File integrity verified. The hash values match.")
else:
print("File integrity verification failed. The hash values do not match.")
except FileNotFoundError:
print("File not found. Please check the file name and try again.")
else:
print("Invalid hash algorithm. Please choose either SHA256, SHA512, SHA1 or md5.")
if choice == "4":
print("Thanks for using the file hasher. Kakfacodes")