Spaces:
Sleeping
Sleeping
File size: 1,223 Bytes
78a2475 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | """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)
|