Skip to content

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.encoding
import std.bytes
import std.crypto
import std.encoding
// Hash a binary file
match 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 binary
match encoding.base64_decode_bytes("aGk=") {
.ok(raw) => println(bytes.to_ints(raw)[0]), // first byte as int
.err(e) => println(e),
}

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.

import std.encoding
encoding.base64_encode("hi") // "aGk="
encoding.base64_decode("aGk=") // .ok("hi")
encoding.base64_decode_bytes("aGk=") // .ok(bytes) — preserves raw bytes
encoding.hex_encode("hi") // "6869"
encoding.hex_decode("6869") // .ok("hi"), odd-length is .err
encoding.url_encode("a b?c") // percent-encoded
encoding.url_decode("a%20b") // .ok("a b")
  • All decode ops return Result<_, str> — invalid input is .err, never panics.
  • hex_decode rejects odd-length input with .err.
  • Text variants require valid UTF-8; use _bytes variants for arbitrary binary.
import std.encoding
import 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)
}
import std.crypto
import 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)
}

7 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.encoding.base64_decode base64_decode(encoded: str) -> Result<str, str> Base64 decode to str
std.encoding.base64_decode_bytes base64_decode_bytes(encoded: str) -> Result<bytes, str> Base64 decode to bytes
std.encoding.base64_encode base64_encode(data: str) -> str Base64 encode
std.encoding.hex_decode hex_decode(encoded: str) -> Result<str, str> Hex decode (odd-length → .err)
std.encoding.hex_encode hex_encode(data: str) -> str Hex encode
std.encoding.url_decode url_decode(encoded: str) -> Result<str, str> Percent-decode
std.encoding.url_encode url_encode(data: str) -> str Percent-encode
  • 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