Light Dark

Functions

random-bytes core

fn (length: Int): Bytes
fn (length: Int, encoding: Str): Str | Bytes

Generate cryptographically secure random bytes. Returns bytes by default, or encoded string if encoding specified.

Example

// Generate 32 random bytes
bytes random-bytes(32)

// Generate as hex string (useful for tokens)
token random-bytes(32, "hex")
// 64-character hex string

// Generate as base64 string
secret random-bytes(32, "base64")
// 44-character base64 string

// Supported encodings: "hex", "base64", "bytes"

random-string core

fn (length: Int): Str

Generate a cryptographically secure random alphanumeric string. Uses secure random for character selection (no modulo bias).

Example

// Generate a 32-character random string
token random-string(32)
// "xK7mP2nQ9rT4vW8yB1cD3fG6hJ0kL5sN"

// Generate API key
api-key random-string(40)

secure-compare core

fn (a: Str | Bytes, b: Str | Bytes): Bool

Compare two strings or byte arrays in constant time to prevent timing attacks. Essential for comparing secrets, tokens, or HMAC signatures.

Example

// Compare secrets safely
is-valid secure-compare(user-token, stored-token)

// Compare HMAC signatures
is-authentic secure-compare(computed-sig, received-sig)