Cryptography (std::crypto)
std::crypto covers unique ID generation and secure randomness. Hash
and HMAC functions live in std::hash (next page). Because it depends on
operating-system entropy, this module is not available in the WASM sandbox —
run the examples with zolo run on the command line.
UUID and NanoID
crypto.uuid() generates a random UUID v4 in the canonical hyphenated format.
crypto.nanoid() produces a 21-character URL-safe identifier — more compact
than UUID and equally unlikely to collide. It accepts a custom size via
nanoid(n).
UUID v4 and NanoID with default and custom sizes.
// Feature: crypto.uuid / crypto.nanoid — unique IDs
// When to use: primary keys, request IDs, session tokens.
use std::crypto
// UUID v4 (random, 128 bits, canonical hyphenated format).
let id = crypto.uuid()
print(id) // expected: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
// nanoid: 21 chars URL-safe — collisions extremely unlikely and more compact than UUID.
let nid = crypto.nanoid()
print(nid.len()) // expected: 21
// nanoid(n) with custom length.
let short = crypto.nanoid(10)
print(short.len()) // expected: 10
Requires the Zolo CLI/host — open in the playground or run locally.
Random bytes and integers
crypto.random_bytes(n) returns an array of n bytes sourced from OS entropy,
suitable for salts and tokens. crypto.random_int(min, max) provides a
uniformly distributed integer in the inclusive range [min, max].
Generating random bytes and range-bounded integers — useful for salts and simulations.
// Feature: crypto.random_bytes / crypto.random_int — secure randomness
// When to use: salt for hashing, secrets, tokens, crypto simulations.
use std::crypto
// random_bytes(n) -> array of N bytes (0..255).
let bytes = crypto.random_bytes(16)
print(bytes.len()) // expected: 16
// random_int(min, max) -> integer in the range [min, max] (inclusive).
let dice = crypto.random_int(1, 6)
print(dice >= 1 && dice <= 6) // expected: true
let port = crypto.random_int(1024, 65535)
print(port >= 1024) // expected: true
Requires the Zolo CLI/host — open in the playground or run locally.
Hashes and HMAC via std::hash
The third file in this folder demonstrates the functions in std::hash
(SHA-256, MD5, HMAC-SHA256 and CRC32). Full documentation is in the next section
(Hash); the example below serves as a cross-reference.
Quick reference for hash.sha256, hash.hmac and hash.crc32.
// Feature: cryptographic hashes via the `hash` module
// When to use: integrity, fingerprints, message authentication.
// Note: `crypto` handles randomness/IDs; hashes live in `hash`.
use std::hash
// SHA-256 — deterministic 32-byte fingerprint (64 hex chars).
print(hash.sha256("zolo")) // expected: 64 hex chars
// SHA-512 / SHA-1 / MD5 also available (legacy: prefer sha256+).
print(hash.sha1("zolo").len()) // expected: 40 (hex of 20 bytes)
print(hash.md5("zolo").len()) // expected: 32 (hex of 16 bytes)
// HMAC-SHA256 — symmetric authentication (message + secret key).
let mac = hash.hmac("sha256", "secret-key", "important message")
print(mac.len()) // expected: 64 hex chars
// CRC32 (non-cryptographic, but useful for cheap checksums).
print(hash.crc32("zolo")) // expected: int
Requires the Zolo CLI/host — open in the playground or run locally.