Files
Onyscribe-Admin/app/services/crypto.py

23 lines
783 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from cryptography.fernet import Fernet
from app.settings import SECRET_KEY
try:
_f = Fernet(SECRET_KEY.encode())
except ValueError as exc:
raise RuntimeError(
"SECRET_KEY ist kein gültiger Fernet-Schlüssel (erwartet: 32 Zufallsbytes in URL-sicherem Base64, 44 Zeichen, endet meist mit „=“). "
"Erzeugen mit: python3 -c \"import base64,os; print(base64.urlsafe_b64encode(os.urandom(32)).decode())\" "
"– und in Coolify als SECRET_KEY setzen (außerhalb von Coolify sichern; ohne ihn sind die Secrets in der Datenbank nicht entschlüsselbar)."
) from exc
def encrypt(v: str) -> str:
return _f.encrypt(v.encode()).decode()
def decrypt(v: str) -> str:
return _f.decrypt(v.encode()).decode()