std.encoding - ZZ
Base64, hex, and URL percent-encoding codecs. *_bytes variants exist because binary payloads cannot round-trip through UTF-8 str. Decode failures are Result.err — never panics.
import std.encodingProduction patterns
Section titled “Production patterns”import std.bytesimport std.cryptoimport std.encoding
// Hash a binary filematch fs.read_bytes("payload.bin") { .ok(b) => { println(crypto.sha256_bytes(b)) // hex digest over raw bytes } .err(e) => println(e),}
// Base64 round-trip preserving binarymatch encoding.base64_decode_bytes("aGk=") { .ok(raw) => println(bytes.to_ints(raw)[0]), // first byte as int .err(e) => println(e),}Purpose
Section titled “Purpose”std.encoding encodes/decodes base64, hex, and URL percent-encoding. Text variants operate on str; _bytes variants operate on bytes for binary-safe round-trips. Use for data transport, embedding binary in text, and URL query building.
Quickstart
Section titled “Quickstart”import std.encoding
encoding.base64_encode("hi") // "aGk="encoding.base64_decode("aGk=") // .ok("hi")encoding.base64_decode_bytes("aGk=") // .ok(bytes) — preserves raw bytesencoding.hex_encode("hi") // "6869"encoding.hex_decode("6869") // .ok("hi"), odd-length is .errencoding.url_encode("a b?c") // percent-encodedencoding.url_decode("a%20b") // .ok("a b")Error model
Section titled “Error model”- All decode ops return
Result<_, str>— invalid input is.err, never panics. hex_decoderejects odd-length input with.err.- Text variants require valid UTF-8; use
_bytesvariants for arbitrary binary.
Production patterns
Section titled “Production patterns”import std.encodingimport std.crypto
func main() -> Result<int, str> { // Encode binary ciphertext for text transport ciphertext_b64 := "aGk=" match encoding.base64_decode_bytes(ciphertext_b64) { .ok(raw) => println(crypto.sha256_bytes(raw)), .err(e) => println(e), }
// Build a URL query query := encoding.url_encode("q=hello world&lang=en") println(query) return .ok(0)}Quickstart
Section titled “Quickstart”import std.cryptoimport std.encoding
func main() -> Result<int, str> { // Digests crypto.sha256("hi") // lowercase hex crypto.sha512("hi") crypto.hmac_sha256("key", "msg")
// Bytes input (binary-safe) match encoding.base64_decode_bytes("aGk=") { .ok(b) => println(crypto.sha256_bytes(b)), // same hex out .err(e) => println(e), }
// Random + constant-time compare crypto.random_bytes(16) // hex of 16 CSPRNG bytes (32 chars) println(crypto.ct_eq("s1", "s1")) // true — never use == for secrets
// Passwords h := crypto.argon2_hash("pw") crypto.argon2_verify(h, "pw") // true crypto.bcrypt_hash("pw") crypto.bcrypt_verify(h, "pw")
// Asymmetric signatures kp := crypto.ed25519_keypair() // [secret, public] sig := crypto.ed25519_sign(kp[0], "msg")? crypto.ed25519_verify(kp[1], "msg", sig)
// JWT tok := crypto.jwt_encode(`{"sub":"1"}`, "secret") _ := crypto.jwt_decode(tok, "secret")? // .ok(payload) / .err _ := crypto.jwt_encode_ed(kp[0], `{"sub":"1"}`)? _ := crypto.jwt_decode_ed(kp[1], tok)?
return .ok(0)}API reference
Section titled “API reference”7 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”std.crypto— Digests, HMAC, random bytes and password hashing.std.uuid— UUID v4 and v7 generation, parsing and validation.
Full index: API reference · Standard library