Skip to content

ob:sqlite, ob:json & ob:crypto

Native modules for persistence, serialization, and cryptography.

ob:sqlite

SQLite is statically bundled inside ob (rusqlite bundled), requiring no external libsqlite3 installation.

  • sqlite.open(path?) — opens ":memory:" (default, no permissions required) or a database file on disk (requires --allow-read and --allow-write).
  • db:exec(sql, params?) — executes DDL/DML statements and returns the number of affected rows.
  • db:query(sql, params?) — executes a SELECT query and returns an array of row tables keyed by column name.
  • db:close() — closes the database connection.
local sqlite = require("ob:sqlite")

local db = sqlite.open(":memory:")
db:exec("CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT, likes INTEGER)")
db:exec("INSERT INTO posts (title, likes) VALUES (?, ?)", { "Orbita v0.1", 42 })

local posts = db:query("SELECT id, title, likes FROM posts WHERE likes > ?", { 10 })
for _, post in ipairs(posts) do
    print(post.id, post.title, post.likes)
end
db:close()

ob:json

Powered by serde_json:

  • json.encode(value) — encodes a Lua table/value into a compact JSON string.
  • json.encode_pretty(value) — encodes into a pretty-printed JSON string.
  • json.decode(str) — parses a JSON string into a Lua table/value.

ob:crypto

  • crypto.sha256(str) — returns the lowercase hex SHA-256 digest (64 characters).
  • crypto.hex_encode(str) — hex-encodes the input string.
  • crypto.random_hex(len?) — generates a cryptographically secure random hex string (default 32 bytes → 64 hex chars), ideal for session IDs and CSRF tokens.
  • crypto.hash_password(password) — hashes a password using Argon2id with a random CSPRNG salt, returning a standard PHC string ($argon2id$...).
  • crypto.verify_password(password, hash) — constant-time verification against an Argon2id PHC hash.
local crypto = require("ob:crypto")

local token = crypto.random_hex(32)
local hash = crypto.hash_password("super-secret-password")

assert(crypto.verify_password("super-secret-password", hash) == true)
assert(crypto.verify_password("wrong", hash) == false)