27 lines
668 B
Python
27 lines
668 B
Python
import hashlib
|
|
import os
|
|
import secrets
|
|
|
|
def hash_password(password: str) -> str:
|
|
salt = secrets.token_hex(16)
|
|
key = hashlib.pbkdf2_hmac(
|
|
'sha256',
|
|
password.encode('utf-8'),
|
|
salt.encode('utf-8'),
|
|
100000
|
|
)
|
|
return f"{salt}${key.hex()}"
|
|
|
|
def verify_password(password: str, hashed: str) -> bool:
|
|
try:
|
|
salt, key_hex = hashed.split('$')
|
|
computed_key = hashlib.pbkdf2_hmac(
|
|
'sha256',
|
|
password.encode('utf-8'),
|
|
salt.encode('utf-8'),
|
|
100000
|
|
)
|
|
return secrets.compare_digest(computed_key.hex(), key_hex)
|
|
except Exception:
|
|
return False
|