from cryptography.fernet import Fernet
from flask import current_app


def _fernet():
    return Fernet(current_app.config["FERNET_KEY"].encode())


def encrypt(plaintext: str) -> bytes:
    if plaintext is None:
        return None
    return _fernet().encrypt(plaintext.encode())


def decrypt(ciphertext: bytes) -> str:
    if ciphertext is None:
        return None
    return _fernet().decrypt(ciphertext).decode()
