"""Argon2id password storage (lab7). Parameters come from the environment and were measured on the stand (student-tests/argon2_bench.py, REPORT.md); the PHC string stores salt + parameters, so old hashes stay valid and are upgraded on the next successful login (needs_rehash).""" from argon2 import PasswordHasher, Type from argon2.exceptions import Argon2Error, InvalidHashError, VerifyMismatchError from app import config HASHER = PasswordHasher(time_cost=config.ARGON2_ITERATIONS, memory_cost=config.ARGON2_MEMORY_KIB, parallelism=config.ARGON2_PARALLELISM, hash_len=32, salt_len=16, type=Type.ID) DUMMY_HASH = HASHER.hash("there-is-no-such-user-password") def hash_password(password: str) -> str: return HASHER.hash(password) def verify_password(stored: str, password: str) -> bool: try: return HASHER.verify(stored, password) except (VerifyMismatchError, Argon2Error, InvalidHashError, ValueError): return False def needs_rehash(stored: str) -> bool: try: return HASHER.check_needs_rehash(stored) except (InvalidHashError, ValueError): return True