before level 3 initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
"""Argon2id password hashes. The hash never leaves this module in an API response."""
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerificationError
|
||||
|
||||
_hasher = PasswordHasher()
|
||||
# Same cost as a real hash, so a missing user is not faster than a wrong password.
|
||||
_DUMMY_HASH = _hasher.hash('not-a-user-password')
|
||||
_DUMMY_PASSWORD = 'not-a-user-password'
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return _hasher.hash(password)
|
||||
|
||||
|
||||
def verify_password(password_hash: str, password: str) -> bool:
|
||||
try:
|
||||
return _hasher.verify(password_hash, password)
|
||||
except VerificationError:
|
||||
return False
|
||||
|
||||
|
||||
def password_matches(stored_hash: str | None, password: str) -> bool:
|
||||
"""One Argon2 check either way. An out-of-range secret is never the stored password."""
|
||||
if stored_hash is None or not 12 <= len(password) <= 128 or not password.strip():
|
||||
verify_password(_DUMMY_HASH, _DUMMY_PASSWORD)
|
||||
return False
|
||||
return verify_password(stored_hash, password)
|
||||
Reference in New Issue
Block a user