-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplebcrypt.py
33 lines (32 loc) · 1.05 KB
/
simplebcrypt.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
def bhash(password):
if type(password) == bytes:
password = password.decode()
if type(password) != str:
raise Exception("Wrong password type: Expected string")
if len(password) > 72:
raise Exception("Password above max length of 72")
try:
import bcrypt
hashed = bcrypt.hashpw(password.encode(),bcrypt.gensalt())
return hashed.decode()
except:
return None
def verify(password,hashed):
if type(password) == bytes:
password = password.decode()
if type(hashed) == bytes:
hashed = hashed.decode()
if type(password) != str or type(hashed) != str:
raise Exception("Wrong password type: Expected string")
if len(password) > 72:
raise Exception("Password is above max length of 72")
try:
import bcrypt
return bcrypt.checkpw(password.encode(),hashed.encode())
except:
return None
if __name__ == '__main__':
passwd = input("Enter a password to test:")
print(bhash(passwd))
print(verify(passwd,bhash(passwd)))
#END