24 lines
671 B
Python
24 lines
671 B
Python
|
|
"""Password hashing using argon2."""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from argon2 import PasswordHasher
|
||
|
|
from argon2.exceptions import VerifyMismatchError
|
||
|
|
|
||
|
|
_hasher = PasswordHasher()
|
||
|
|
|
||
|
|
|
||
|
|
def hash_password(password: str) -> str:
|
||
|
|
"""Return an argon2 hash of `password`."""
|
||
|
|
return _hasher.hash(password)
|
||
|
|
|
||
|
|
|
||
|
|
def verify_password(password_hash: str, password: str) -> bool:
|
||
|
|
"""Return True iff `password` matches `password_hash`."""
|
||
|
|
try:
|
||
|
|
return _hasher.verify(password_hash, password)
|
||
|
|
except VerifyMismatchError:
|
||
|
|
return False
|
||
|
|
except Exception:
|
||
|
|
# Malformed hash or other argon2 error — treat as auth failure.
|
||
|
|
return False
|