Ushka

ushka.crypto.fernet

Fernet encryption: encrypt, decrypt, generate_key and key rotation.

A token is authenticated (AES-128-CBC plus HMAC-SHA256, the Fernet recipe of the cryptography package) and carries the time it was made, so a changed token, a wrong key and an expired token all fail the same way: with DecryptionError, whose message says nothing about the content or the reason.

Keys come from [crypto] keys (USHKA_CRYPTO_KEYS, comma separated). The first key encrypts, every key decrypts: to rotate, put the new key first and keep the old one until nothing encrypted with it is left. Without keys, a key is derived from [app] secret_key (HKDF-SHA256), so encryption works with no extra setup; derive_key shows how.

Decrypted values are plain str, never Untrusted: a token that decrypts was written by this application (nobody else holds the key). If what you encrypted was request data and it reaches a sink again, wrap it: Untrusted(text, "db").

In this module

class DecryptionErrorThe token could not be decrypted: changed, truncated, expired, or made with another key. The message is the same for all of them on purpose.
def is_available()True when the crypto extra is installed.
def generate_key()A new random key (urlsafe base64 of 32 bytes), ready for USHKA_CRYPTO_KEYS.
def derive_key()The key Ushka derives from [app] secret_key when no keys are set: HKDF-SHA256 with the fixed info b"ushka-fernet-v1", no salt, 32 bytes.
def keys_from_config()The keys of an app: [crypto] keys, or the one derived from [app] secret_key.
def uses_derived_key()True when encryption would use the key derived from secret_key (no [crypto] keys).
def validate_config()Fails now, with a clear message, if encryption cannot work: the extra is missing, there is no key, or a key is malformed. The app calls it…
def encrypt_bytes()Encrypts data and returns the Fernet token (urlsafe text).
def decrypt_bytes()Decrypts a token from encrypt_bytes. With ttl, a token older than that many seconds is refused. Raises DecryptionError for any bad token.
def encrypt()Encrypts text and returns the Fernet token (urlsafe text).
def decrypt()Decrypts a token from encrypt and returns a plain str. Raises DecryptionError.

class DecryptionError (Exception)

The token could not be decrypted: changed, truncated, expired, or made with another key. The message is the same for all of them on purpose.

def is_available

def is_available() -> bool

True when the crypto extra is installed.

def generate_key

def generate_key() -> str

A new random key (urlsafe base64 of 32 bytes), ready for USHKA_CRYPTO_KEYS.

def derive_key

def derive_key(secret_key: str) -> str

The key Ushka derives from [app] secret_key when no keys are set: HKDF-SHA256 with the fixed info b"ushka-fernet-v1", no salt, 32 bytes.

It is stable (same secret, same key) and different from the secret itself. Use it to move to a dedicated key: keys = [new, derive_key(secret_key)].

def keys_from_config

def keys_from_config(config: Any) -> list[str]

The keys of an app: [crypto] keys, or the one derived from [app] secret_key.

With [crypto] keep_derived the derived key is also kept, last, so data written before the move to a dedicated key can still be read.

def uses_derived_key

def uses_derived_key(config: Any) -> bool

True when encryption would use the key derived from secret_key (no [crypto] keys).

def validate_config

def validate_config(config: Any) -> None

Fails now, with a clear message, if encryption cannot work: the extra is missing, there is no key, or a key is malformed. The app calls it at startup when a feature that encrypts is on.

def encrypt_bytes

def encrypt_bytes(data: bytes, *, key: Any = None) -> str

Encrypts data and returns the Fernet token (urlsafe text).

key is a key or a list of keys (the first encrypts); omitted, the keys of the app are used.

def decrypt_bytes

def decrypt_bytes(
    token: str | bytes,
    *,
    key: Any = None,
    ttl: int | None = None,
) -> bytes

Decrypts a token from encrypt_bytes. With ttl, a token older than that many seconds is refused. Raises DecryptionError for any bad token.

def encrypt

def encrypt(data: str, *, key: Any = None) -> str

Encrypts text and returns the Fernet token (urlsafe text).

token = encrypt("secret") # keys from [crypto] / [app] secret_key decrypt(token) 'secret'

def decrypt

def decrypt(
    token: str | bytes,
    *,
    key: Any = None,
    ttl: int | None = None,
) -> str

Decrypts a token from encrypt and returns a plain str. Raises DecryptionError.