| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- """
- Encryption utilities for sensitive data (WiFi passwords, etc).
- Uses Fernet (symmetric encryption) from cryptography library.
- """
- from cryptography.fernet import Fernet
- from app.config import settings
- def get_cipher() -> Fernet:
- """Get Fernet cipher instance."""
- # Convert settings key to bytes if needed
- key = settings.ENCRYPTION_KEY
- if isinstance(key, str):
- key = key.encode()
- return Fernet(key)
- def encrypt_password(plain_password: str) -> str:
- """
- Encrypt a password using Fernet.
- Args:
- plain_password: Plain text password
- Returns:
- Encrypted password as string (base64 encoded)
- """
- cipher = get_cipher()
- encrypted_bytes = cipher.encrypt(plain_password.encode())
- return encrypted_bytes.decode()
- def decrypt_password(encrypted_password: str) -> str:
- """
- Decrypt a password using Fernet.
- Args:
- encrypted_password: Encrypted password (base64 encoded string)
- Returns:
- Decrypted plain text password
- """
- cipher = get_cipher()
- decrypted_bytes = cipher.decrypt(encrypted_password.encode())
- return decrypted_bytes.decode()
|