Criptografia (std::crypto)
Nesta página
std::crypto cobre geração de IDs únicos e aleatoriedade segura. Funções
de hash e HMAC vivem em std::hash (página seguinte). Por depender de
entropia do sistema operacional, este módulo não está disponível no sandbox WASM
— execute os exemplos com zolo run na linha de comando.
UUID e NanoID
crypto.uuid() gera um UUID v4 aleatório no formato canônico com hifens.
crypto.nanoid() produz um identificador URL-safe de 21 caracteres — mais compacto
que UUID e igualmente improvável de colidir. Aceita tamanho customizado via
nanoid(n).
UUID v4 e NanoID de tamanho padrão e personalizado.
// Feature: crypto.uuid / crypto.nanoid — unique IDs
// When to use: primary keys, request IDs, session tokens.
// parity-normalize: uuid
// crypto.uuid() is random per run; the canonical UUID is collapsed to <UUID>
// on both backends. A `nil` (unbridged) would NOT match <UUID>, so a real
// native regression still diverges. nanoid lengths (21/10) stay exact.
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
Requer a CLI/host do Zolo — abra no playground ou rode localmente.
Bytes e inteiros aleatórios
crypto.random_bytes(n) retorna um array de n bytes com entropia do SO,
adequado para salts e tokens. crypto.random_int(min, max) fornece um inteiro
uniformemente distribuído no intervalo [min, max] inclusivo.
Geração de bytes aleatórios e inteiros em faixa — útil para salts e simulações.
// 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
Requer a CLI/host do Zolo — abra no playground ou rode localmente.
Hashes e HMAC via std::hash
O terceiro arquivo desta pasta demonstra as funções de std::hash (SHA-256, MD5,
HMAC-SHA256 e CRC32). O conteúdo completo está documentado na próxima seção
(Hash); o exemplo abaixo serve como referência cruzada.
Referência rápida a hash.sha256, hash.hmac e 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
Requer a CLI/host do Zolo — abra no playground ou rode localmente.