Skip to main content

cache_key

The SQLCipher cache key, derived from the desktop app's encryption key.

The desktop app keeps one AES-GCM-256 key in the OS keychain (service Bitfount, account ENCRYPTION::KEY), stored as a JSON Web Key. The caches never use that key directly; they use keys derived from it:

KeyDerived fromHKDF infoLength
cache keythe JWK's kCACHE_KEY_INFO32
key id (public)cache keyKEY_ID_INFO8, as hex

Both derivations are HKDF-SHA256 with an empty salt. The desktop app derives the same two values, so the labels and lengths are a contract with it: changing either strands every encrypted cache.

Module​

Functions​

cache_key_from_hex​

def cache_key_from_hex(value: str) ‑> CacheKey:

Parses an explicit cache key, as BITFOUNT_CACHE_ENCRYPTION_KEY holds it.

Arguments

  • value: The key as 64 hex characters, optionally prefixed 0x or 0X and surrounded by whitespace.

Returns The cache key.

Raises

  • InvalidCacheKeyError: If value isn't 64 hex characters. The message never includes any part of value.

cache_key_from_jwk​

def cache_key_from_jwk(jwk_json: str | bytes) ‑> bytes:

Derives the SQLCipher cache key from the desktop app's encryption key.

Arguments

  • jwk_json: The keychain item's value: a JSON Web Key holding a 32-byte symmetric key, as text or as the UTF-8 bytes the keychain readers return. Pass the bytes as they are: decoding them first raises a UnicodeDecodeError that holds the key.

Returns The 32-byte cache key.

Raises

  • InvalidCacheKeyError: If jwk_json isn't a JWK of type oct with a 32-byte k. The message never includes any part of the key.

cache_key_id​

def cache_key_id(cache_key: bytes) ‑> str:

Derives the public id of a cache key.

The id names which key encrypted a cache without revealing the key, so it can appear in logs, telemetry and error messages.

Arguments

  • cache_key: The 32-byte cache key.

Returns 16 lowercase hex characters.

Raises

  • InvalidCacheKeyError: If cache_key is not 32 bytes.

Classes​

CacheKey​

class CacheKey(key: bytes):

A 32-byte SQLCipher cache key that keeps itself out of logs and pickles.

It prints as its public key id, and refuses to be pickled or copied, so it can't reach a log line, a multiprocessing argument or a Prefect parameter by accident. secret is the only way to the bytes.

Wraps a cache key.

Arguments

  • key: The 32-byte cache key.

Raises

  • InvalidCacheKeyError: If key is not 32 bytes.

Variables​

  • key_id : str - The key's public id: 16 lowercase hex characters.

Methods​


secret​

def secret(self) ‑> bytes:

The key's 32 bytes.