This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""Пароли: Argon2id. Токены: SHA-256 от случайного секрета."""
|
||||
|
||||
import hashlib
|
||||
|
||||
from anyio import CapacityLimiter, to_thread
|
||||
from argon2 import PasswordHasher, Type
|
||||
from argon2.exceptions import InvalidHashError, VerificationError
|
||||
|
||||
from src.core.settings import settings
|
||||
|
||||
password_hasher = PasswordHasher(
|
||||
time_cost=settings.argon2_time_cost,
|
||||
memory_cost=settings.argon2_memory_cost,
|
||||
parallelism=settings.argon2_parallelism,
|
||||
salt_len=settings.argon2_salt_len,
|
||||
hash_len=settings.argon2_hash_len,
|
||||
type=Type.ID,
|
||||
)
|
||||
password_limiter = CapacityLimiter(settings.password_hash_concurrency)
|
||||
|
||||
|
||||
async def hash_password_async(password: str) -> str:
|
||||
return await to_thread.run_sync(hash_password, password, limiter=password_limiter)
|
||||
|
||||
|
||||
async def verify_password_async(password: str, stored: str) -> bool:
|
||||
return await to_thread.run_sync(verify_password, password, stored, limiter=password_limiter)
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return password_hasher.hash(password)
|
||||
|
||||
|
||||
def verify_password(password: str, stored: str) -> bool:
|
||||
try:
|
||||
return password_hasher.verify(stored, password)
|
||||
except (VerificationError, InvalidHashError):
|
||||
return False
|
||||
|
||||
|
||||
def hash_token(token: str) -> str:
|
||||
return hashlib.sha256(token.encode("utf-8")).hexdigest()
|
||||
Reference in New Issue
Block a user