module

Kemal::Session::Crypto

Low-level cryptographic primitives matching Rails' defaults: PBKDF2-HMAC-SHA1 key derivation and AES-256-GCM authenticated encryption.

Constants

AUTH_TAG_LEN = 16
CIPHER_NAME = "aes-256-gcm"

Not configurable, and shouldn't be. Rails' encrypted_cookie_cipher can technically be changed, but its only other value (aes-256-cbc) is a different wire format there — non-AEAD ciphers get an HMAC signature appended and no auth tag — so it would be a second code path, not a different constant. encrypt also sizes its output buffer at exactly plaintext.size, which is only correct for a stream mode like GCM.

IV_LEN = 12
KEY_LEN = 32

Instance methods

decrypt(key : Bytes, iv : Bytes, ciphertext : Bytes, tag : Bytes) : Bytes

Decrypts and authenticates an AES-256-GCM message. Raises Crypto::Error if the auth tag does not verify (tampering, wrong key, etc.).

Source
derive_key(secret : String, salt : String, iterations : Int32 = 1000, key_len : Int32 = KEY_LEN) : Bytes

PBKDF2-HMAC-SHA1, matching ActiveSupport::KeyGenerator (iterations: 1000, SHA1, 32-byte key for AES-256-GCM cookies).

Source
encrypt(key : Bytes, iv : Bytes, plaintext : Bytes) : Tuple(Bytes, Bytes)

Encrypts plaintext with AES-256-GCM. Returns {ciphertext, auth_tag}. No additional authenticated data is used (matching Rails cookies).

Source

Nested types