Files
mtusi2026lab7/app/security.py
T

21 lines
700 B
Python

"""Password KDF (Argon2id) — never store or compare plaintext passwords."""
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError, InvalidHashError
# OWASP-aligned lab budget: memory_cost is KiB (19 MiB), t=2, p=1.
_hasher = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
def hash_password(password: str) -> str:
return _hasher.hash(password)
def verify_password(password: str, password_hash: str) -> bool:
try:
return _hasher.verify(password_hash, password)
except (VerifyMismatchError, InvalidHashError):
return False
def needs_rehash(password_hash: str) -> bool:
return _hasher.check_needs_rehash(password_hash)