Spaces:
Sleeping
Sleeping
Aditya "Sree" Sreevatsa K
fix: harden snippet utilities and restore dashboard functionality (#1)
78a2475 unverified Download File_Handling/encrypt_decrypt_files.py from AdityaSreevatsaK/Utility-Shelf: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/spaces/AdityaSreevatsaK/Utility-Shelf/resolve/main/File_Handling/encrypt_decrypt_files.py
- Command line
-
hf download hf://spaces/AdityaSreevatsaK/Utility-Shelf/File_Handling/encrypt_decrypt_files.py
-
curl -L -o encrypt_decrypt_files.py https://huggingface.co/spaces/AdityaSreevatsaK/Utility-Shelf/resolve/main/File_Handling/encrypt_decrypt_files.py
1.22 kB
| """Explicit Fernet encryption helpers. | |
| Keys are intentionally never generated or saved during module import. | |
| Do not commit generated keys to a public repository. | |
| """ | |
| from pathlib import Path | |
| from cryptography.fernet import Fernet | |
| def generate_key() -> bytes: | |
| """Generate a key that should be stored in a secure secrets manager.""" | |
| return Fernet.generate_key() | |
| def encrypt_file(source_path: str, output_path: str, key: bytes) -> None: | |
| """Encrypt file bytes using the supplied Fernet key.""" | |
| source = Path(source_path) | |
| destination = Path(output_path) | |
| if source.resolve() == destination.resolve(): | |
| raise ValueError("Source and destination must be different") | |
| payload = Fernet(key).encrypt(source.read_bytes()) | |
| destination.write_bytes(payload) | |
| def decrypt_file(source_path: str, output_path: str, key: bytes) -> None: | |
| """Decrypt a Fernet-encrypted file, or raise InvalidToken for a wrong key.""" | |
| source = Path(source_path) | |
| destination = Path(output_path) | |
| if source.resolve() == destination.resolve(): | |
| raise ValueError("Source and destination must be different") | |
| payload = Fernet(key).decrypt(source.read_bytes()) | |
| destination.write_bytes(payload) | |