Spaces:
Sleeping
Sleeping
Download src/utils/encryption.py from hashir-qt/keyvault-api: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/hashir-qt/keyvault-api/resolve/main/src/utils/encryption.py
- Command line
-
hf download hf://spaces/hashir-qt/keyvault-api/src/utils/encryption.py
-
curl -L -o encryption.py https://huggingface.co/spaces/hashir-qt/keyvault-api/resolve/main/src/utils/encryption.py
1.1 kB
| import base64 | |
| from cryptography.fernet import Fernet | |
| from src.config import settings | |
| def _get_fernet() -> Fernet: | |
| """Build a Fernet instance from the configured ENCRYPTION_KEY.""" | |
| key_bytes = settings.encryption_key.encode() | |
| # Fernet requires a 32-byte URL-safe base64-encoded key | |
| padded = key_bytes[:32].ljust(32, b"=") | |
| encoded_key = base64.urlsafe_b64encode(padded) | |
| return Fernet(encoded_key) | |
| def encrypt_key(plain_key: str) -> str: | |
| """ | |
| Encrypt a product key for safe storage in the database. | |
| Args: | |
| plain_key: The raw product/game key. | |
| Returns: | |
| Base64-encoded encrypted string. | |
| """ | |
| fernet = _get_fernet() | |
| return fernet.encrypt(plain_key.encode()).decode() | |
| def decrypt_key(encrypted_key: str) -> str: | |
| """ | |
| Decrypt a product key retrieved from the database. | |
| Args: | |
| encrypted_key: The stored encrypted key string. | |
| Returns: | |
| The original plain-text product key. | |
| """ | |
| fernet = _get_fernet() | |
| return fernet.decrypt(encrypted_key.encode()).decode() | |