Лимит входа общий для реплик, размер тела ограничен, исходящие клиенты проверяют TLS, если задан CA. Co-authored-by: Cursor <cursoragent@cursor.com>
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import os
|
|
|
|
from argon2 import PasswordHasher
|
|
from argon2.exceptions import InvalidHashError, VerifyMismatchError
|
|
|
|
# 19 MiB is the lab floor. Salt and parameters live inside the PHC string.
|
|
MEMORY_KIB = int(os.environ.get("ARGON2_MEMORY_KIB") or "19456")
|
|
ITERATIONS = int(os.environ.get("ARGON2_ITERATIONS") or "2")
|
|
PARALLELISM = int(os.environ.get("ARGON2_PARALLELISM") or "1")
|
|
|
|
hasher = PasswordHasher(
|
|
time_cost=ITERATIONS,
|
|
memory_cost=MEMORY_KIB,
|
|
parallelism=PARALLELISM,
|
|
hash_len=32,
|
|
salt_len=16,
|
|
)
|
|
_dummy = None
|
|
|
|
|
|
def hash_password(password: str) -> str:
|
|
return hasher.hash(password)
|
|
|
|
|
|
def dummy_hash() -> str:
|
|
global _dummy
|
|
if _dummy is None:
|
|
_dummy = hash_password("timing-dummy-password")
|
|
return _dummy
|
|
|
|
|
|
def verify_password(password_hash: str, password: str) -> bool:
|
|
try:
|
|
return hasher.verify(password_hash, password)
|
|
except (VerifyMismatchError, InvalidHashError, TypeError, ValueError):
|
|
return False
|
|
|
|
|
|
def needs_rehash(password_hash: str) -> bool:
|
|
try:
|
|
return hasher.check_needs_rehash(password_hash)
|
|
except (InvalidHashError, TypeError, ValueError):
|
|
return False
|