| 1234567891011121314151617181920212223 |
- from passlib.context import CryptContext
- from cryptography.fernet import Fernet
- import os
- # Хэширование паролей
- pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
- def hash_password(password: str) -> str:
- return pwd_context.hash(password)
- def verify_password(plain_password: str, hashed_password: str) -> bool:
- return pwd_context.verify(plain_password, hashed_password)
- # Шифрование (например, для email)
- # Генерируем ключ, в продакшене хранить securely
- key = os.getenv("ENCRYPTION_KEY", Fernet.generate_key())
- cipher = Fernet(key)
- def encrypt_data(data: str) -> str:
- return cipher.encrypt(data.encode()).decode()
- def decrypt_data(encrypted_data: str) -> str:
- return cipher.decrypt(encrypted_data.encode()).decode()
|