20 lines
513 B
Python
20 lines
513 B
Python
from cryptography.fernet import Fernet
|
|
from app.core.config import get_settings
|
|
|
|
|
|
def _get_fernet() -> Fernet:
|
|
settings = get_settings()
|
|
key = settings.SECRET_KEY.encode("utf-8")
|
|
key = key.ljust(44, b"=")[:44]
|
|
return Fernet(key)
|
|
|
|
|
|
def encrypt_api_key(api_key: str) -> str:
|
|
f = _get_fernet()
|
|
return f.encrypt(api_key.encode("utf-8")).decode("utf-8")
|
|
|
|
|
|
def decrypt_api_key(encrypted_key: str) -> str:
|
|
f = _get_fernet()
|
|
return f.decrypt(encrypted_key.encode("utf-8")).decode("utf-8")
|