-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.py
157 lines (132 loc) · 6.66 KB
/
hash.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
Hash
Programmed by: Paramon Yevstigneyev
Programmed in: Python 3.8.10 (64-Bit)
Decription:
This is used to hash a string a user inputted, in a hashing algorithm the user selected.
"""
class user_string:
"""
A method used for encrypting a string with
common encryption algorithms.
"""
# Used for hashing user strings.
import hashlib
def __init__(self):
pass
def md5(self, string, salt):
"""
Hashes a string using the MD5 Hashing Algorithm.
"""
# If the ueser does not specify any salt to add to their hash, then it will hash the string provided.
if salt == None:
self.md5_hasher = self.hashlib.md5()
self.md5_hasher.update(string.encode("utf-8"))
self.hashed_string = self.md5_hasher.hexdigest()
return self.hashed_string
# If the user does specify a salt to add to their hash, then it will combine the string and salt before it gets hashed.
# It will give the user a salted hash and a unsalted hash.
else:
self.md5_hasher = self.hashlib.md5()
self.md5_hasher.update(string.encode("utf-8"))
self.hashed_string = self.md5_hasher.hexdigest()
self.md5_hasher = self.hashlib.md5()
self.md5_hasher.update(f"{string}{salt}".encode("utf-8"))
self.salted_hash = self.md5_hasher.hexdigest()
return self.hashed_string, self.salted_hash
def sha1(self, string, salt):
"""
Hashes a string using the SHA-1 Hashing Algorithm.
"""
# If the ueser does not specify any salt to add to their hash, then it will hash the string provided.
if salt == None:
self.sha1_hasher = self.hashlib.sha1()
self.sha1_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha1_hasher.hexdigest()
return self.hashed_string
# If the user does specify a salt to add to their hash, then it will combine the string and salt before it gets hashed.
# It will give the user a salted hash and a unsalted hash.
else:
self.sha1_hasher = self.hashlib.sha1()
self.sha1_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha1_hasher.hexdigest()
self.sha1_hasher = self.hashlib.sha1()
self.sha1_hasher.update(f"{string}{salt}".encode("utf-8"))
self.salted_hash = self.sha1_hasher.hexdigest()
return self.hashed_string, self.salted_hash
def sha224(self, string, salt):
"""
Hashes a string using the SHA-224 Hashing Algorithm.
"""
# If the ueser does not specify any salt to add to their hash, then it will hash the string provided.
if salt == None:
self.sha224_hasher = self.hashlib.sha224()
self.sha224_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha224_hasher.hexdigest()
return self.hashed_string
# If the user does specify a salt to add to their hash, then it will combine the string and salt before it gets hashed.
# It will give the user a salted hash and a unsalted hash.
else:
self.sha224_hasher = self.hashlib.sha224()
self.sha224_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha224_hasher.hexdigest()
self.sha224_hasher = self.hashlib.sha224()
self.sha224_hasher.update(f"{string}{salt}".encode("utf-8"))
self.salted_hashed = self.sha224_hasher.hexdigest()
return self.hashed_string, self.salted_hash
def sha256(self, string, salt):
"""
Hashes a string using the SHA-256 Hashing Algorithm.
"""
# If the ueser does not specify any salt to add to their hash, then it will hash the string provided.
if salt == None:
self.sha256_hasher = self.hashlib.sha256()
self.sha256_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha256_hasher.hexdigest()
return self.hashed_string
# If the user does specify a salt to add to their hash, then it will combine the string and salt before it gets hashed.
# It will give the user a salted hash and a unsalted hash.
else:
self.sha256_hasher = self.hashlib.sha256()
self.sha256_hasher.update(f"{string}{salt}".encode("utf-8"))
self.salted_hash = self.sha256_hasher.hexdigest()
return self.hashed_string, self.salted_hash
def sha384(self, string, salt):
"""
Hashes a string using the SHA-384 Hashing Algorithm.
"""
# If the ueser does not specify any salt to add to their hash, then it will hash the string provided.
if salt == None:
self.sha384_hasher = self.hashlib.sha384()
self.sha384_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha384_hasher.hexdigest()
return self.hashed_string
# If the user does specify a salt to add to their hash, then it will combine the string and salt before it gets hashed.
# It will give the user a salted hash and a unsalted hash.
else:
self.sha384_hasher = self.hashlib.sha384()
self.sha384_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha384_hasher.hexdigest()
self.sha384_hasher = self.hashlib.sha384()
self.sha384_hasher.update(f"{string}{salt}".encode("utf-8"))
self.salted_hash = self.sha384_hasher.hexdigest()
return self.hashed_string, self.salted_hash
def sha512(self, string, salt):
"""
Hashes a string using the SHA-512 Hashing Algorithm.
"""
if salt == None:
self.sha512_hasher = self.hashlib.sha512()
self.sha512_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha512_hasher.hexdigest()
return self.hashed_string
# If the user does specify a salt to add to their hash, then it will combine the string and salt before it gets hashed.
# It will give the user a salted hash and a unsalted hash.
else:
self.sha512_hasher = self.hashlib.sha512()
self.sha512_hasher.update(string.encode("utf-8"))
self.hashed_string = self.sha512_hasher.hexdigest()
self.sha512_hasher = self.hashlib.sha512()
self.sha512_hasher.update(f"{string}{salt}".encode("utf-8"))
self.salted_hash = self.sha512_hasher.hexdigest()
return self.hashed_string, self.salted_hash