23 lines
783 B
Python
23 lines
783 B
Python
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()
|