Skip to content

Encryption & Signing

Cryptographic operations including encryption, HMAC, JWT, and key derivation. Adapted for workflows.

local crypto = require("crypto")
local bytes, err = crypto.random.bytes(32)
ParameterTypeDescription
lengthintegerNumber of bytes (1 to 1,048,576)

Returns: string, error

local str, err = crypto.random.string(32)
local str, err = crypto.random.string(32, "0123456789abcdef")
ParameterTypeDescription
lengthintegerString length (1 to 1,048,576)
charsetstring?Characters to use (default: alphanumeric)

Returns: string, error

local id, err = crypto.random.uuid()

Returns: string, error

local hex, err = crypto.hmac.sha256(key, data)
ParameterTypeDescription
keystringHMAC key
datastringData to authenticate

Returns: string, error

local hex, err = crypto.hmac.sha512(key, data)
ParameterTypeDescription
keystringHMAC key
datastringData to authenticate

Returns: string, error

local encrypted, err = crypto.encrypt.aes(data, key)
local encrypted, err = crypto.encrypt.aes(data, key, aad)
ParameterTypeDescription
datastringPlaintext to encrypt
keystring16, 24, or 32 bytes (AES-128/192/256)
aadstring?Additional authenticated data

Returns: string, error (nonce prepended)

ChaCha20-Poly1305 {id=“encrypt-chacha20”}

Section titled “ChaCha20-Poly1305 {id=“encrypt-chacha20”}”
local encrypted, err = crypto.encrypt.chacha20(data, key)
local encrypted, err = crypto.encrypt.chacha20(data, key, aad)
ParameterTypeDescription
datastringPlaintext to encrypt
keystringMust be 32 bytes
aadstring?Additional authenticated data

Returns: string, error

local plaintext, err = crypto.decrypt.aes(encrypted, key)
local plaintext, err = crypto.decrypt.aes(encrypted, key, aad)
ParameterTypeDescription
datastringEncrypted data from encrypt.aes
keystringSame key used for encryption
aadstring?Must match AAD used in encryption

Returns: string, error

ChaCha20-Poly1305 {id=“decrypt-chacha20”}

Section titled “ChaCha20-Poly1305 {id=“decrypt-chacha20”}”
local plaintext, err = crypto.decrypt.chacha20(encrypted, key)
local plaintext, err = crypto.decrypt.chacha20(encrypted, key, aad)
ParameterTypeDescription
datastringEncrypted data from encrypt.chacha20
keystringSame key used for encryption
aadstring?Must match AAD used in encryption

Returns: string, error

local token, err = crypto.jwt.encode(payload, secret)
local token, err = crypto.jwt.encode(payload, secret, "HS256")
local token, err = crypto.jwt.encode(payload, private_key_pem, "RS256")
ParameterTypeDescription
payloadtableJWT claims (_header for custom header)
keystringSecret (HMAC) or PEM private key (RSA)
algstring?HS256, HS384, HS512, RS256 (default: HS256)

Returns: string, error

local claims, err = crypto.jwt.verify(token, secret)
local claims, err = crypto.jwt.verify(token, secret, "HS256", false)
local claims, err = crypto.jwt.verify(token, public_key_pem, "RS256")
ParameterTypeDescription
tokenstringJWT token to verify
keystringSecret (HMAC) or PEM public key (RSA)
algstring?Expected algorithm (default: HS256)
require_expboolean?Validate expiration (default: true)

Returns: table, error

local key, err = crypto.pbkdf2(password, salt, iterations, key_length)
local key, err = crypto.pbkdf2(password, salt, iterations, key_length, "sha512")
ParameterTypeDescription
passwordstringPassword/passphrase
saltstringSalt value
iterationsintegerIteration count (max 10,000,000)
key_lengthintegerDesired key length in bytes
hashstring?sha256 or sha512 (default: sha256)

Returns: string, error

local equal = crypto.constant_time_compare(a, b)
ParameterTypeDescription
astringFirst string
bstringSecond string

Returns: boolean

ConditionKindRetryable
Invalid lengtherrors.INVALIDno
Empty keyerrors.INVALIDno
Invalid key sizeerrors.INVALIDno
Decryption failederrors.INTERNALno
Token expirederrors.INTERNALno

See Error Handling for working with errors.