utils.py 822 B

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