Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Ciphers (AEAD)

Cipher binds algorithm + matching Key. Encrypt/decrypt take a params DynamicObject.

ConstantNotes
Cipher.aes256gcmPrefer on hosts with AES-NI; 12-byte nonce
Cipher.chacha20poly1305Strong default without AES-NI; 12-byte nonce
KindNameEffect
classnew( algo:s key:Key -- cipher )
methodnonceSize( -- n:i )
methodgenerateNonce( -- nonce:ByteList )
methodencrypt( params:DO -- ciphertext:ByteList )
methoddecrypt( params:DO -- plaintext true | err:s false )

Encrypt fields: plaintext (req), nonce (req), aad (opt).
Decrypt fields: ciphertext (req), nonce (req), aad (opt).
Auth failure: no partial plaintext; modal false.

: demoAead
  msgBytes >zs
  Key.aes256gcm Key.generate^
  Cipher.aes256gcm swap Cipher.new^ >ts
  t:.generateNonce^ >xs
  { z:peek "plaintext" x:peek "nonce" } t:.encrypt^ >ys
  { y:peek "ciphertext" x:peek "nonce" } t:.decrypt^
  nif
    "decrypt failed: " $ $ nl
    error
  then
  // plaintext:ByteList
  t:drop x:drop y:drop z:drop
;

Same API for ChaCha20-Poly1305: Key.chacha20poly1305 + Cipher.chacha20poly1305.

Full example: examples/crypto/1a-seal-and-sign.fnl.