-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
26 lines (22 loc) · 850 Bytes
/
utils.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
import bcrypt
# Hash a password using bcrypt
def hash_password(password):
pwd_bytes = password.encode("utf-8")
salt = bcrypt.gensalt()
hashed_password = bcrypt.hashpw(password=pwd_bytes, salt=salt)
return hashed_password
# Check if the provided password matches the stored password (hashed)
def verify_password(plain_password, hashed_password) -> bool:
password_byte_enc = bytes(plain_password.encode("utf-8"))
try:
if not isinstance(hashed_password, (bytes, bytearray)):
hashed_byte_enc = bytes(hashed_password.encode("utf-8"))
hashed_password = hashed_byte_enc
except (UnicodeDecodeError, AttributeError):
pass
try:
return bcrypt.checkpw(
password=password_byte_enc, hashed_password=hashed_password
)
except Exception:
return False