20 lines
646 B
Python
20 lines
646 B
Python
import hashlib
|
|
import secrets
|
|
|
|
|
|
class PasswordHasher:
|
|
"""
|
|
Класс для безопасного хэширования паролей с использованием соли.
|
|
"""
|
|
|
|
def generate_salt(self, length: int = 16) -> str:
|
|
return secrets.token_hex(length)
|
|
|
|
def hash_password(self, password: str, salt: str) -> str:
|
|
hasher = hashlib.sha256()
|
|
hasher.update(salt.encode("utf-8") + password.encode("utf-8"))
|
|
return hasher.hexdigest()
|
|
|
|
def verify_password(self, password: str, salt: str, expected_hash: str) -> bool:
|
|
return self.hash_password(password, salt) == expected_hash
|