Base64, Hex and Base32 (std::base64)
When bytes need to be transported in text-only contexts — JSON, URLs, MIME
e-mail — use the stdlib binary encodings. std::base64 covers the standard and
URL-safe alphabets; std::hex and std::base32 round out the most common
alternatives. All examples are pure computations and run in the sandbox.
Standard encode and decode
base64.encode converts a string to Base64 with = padding. base64.decode
reverses the operation. The padding table for strings of 1, 2 and 3 bytes
illustrates alignment into four-character blocks.
Standard encoding with padding and decoding back to the original string.
// Feature: base64.encode / base64.decode — encode binary into ASCII text
// When to use: embed bytes in JSON, in URLs, in emails (MIME).
use std::base64
let raw = "hello, zolo!"
let enc = base64.encode(raw)
print(enc) // expected: aGVsbG8sIHpvbG8h
// decode reverses. Note: decode returns a string (interpreted as UTF-8).
let back = base64.decode(enc)
print(back) // expected: hello, zolo!
// Padding `=` is included by default in the standard variant.
print(base64.encode("a")) // expected: YQ==
print(base64.encode("ab")) // expected: YWI=
print(base64.encode("abc")) // expected: YWJj
URL-safe variant
The standard alphabet uses +, / and =, which have special meaning in
URLs. base64.encode_url replaces + with -, / with _ and omits the
padding — the result can go directly into a query string or a JWT.
Comparison between standard encode and encode_url for safe use in URLs.
// Feature: base64.encode_url — URL-safe variant (no +, /, =)
// When to use: tokens in query strings, JWT-like payloads, file names.
// Differences: uses - instead of + and _ instead of /, no = padding.
use std::base64
let raw = "subjects?id=42&active=true"
let std = base64.encode(raw)
let url = base64.encode_url(raw)
print(std) // expected: contains + or /, ends with =
print(url) // expected: uses - or _, no =
// Standard base64 can break URLs because of +, / and =.
// The URL-safe variant is safe to put in a query string without extra encoding.
Hex and Base32
std::hex produces two lowercase characters per byte — easy to read in logs
and debuggers. std::base32 uses the Crockford alphabet (5 bits per char)
without the ambiguous characters 0/O and 1/I, making it suitable for
human-readable identifiers.
Hex encoding/decoding and Base32 with decode_string back to the original.
// Feature: hex / base32 — alternative encodings
// When to use:
// hex -> debug, fingerprints, human-readable hashes
// base32 -> short identifiers without ambiguity (no 0/O, 1/I)
use std::hex
use std::base32
// hex.encode produces a string with 2 chars per byte.
let raw = "hello"
print(hex.encode(raw)) // expected: 68656c6c6f
// hex.decode returns an array of bytes; decode_string reassembles the string.
print(hex.decode_string("68656c6c6f")) // expected: hello
// base32 — Crockford alphabet (5 bits per char).
let b32 = base32.encode("zolo")
print(b32) // expected: PJXWQZ3R (or similar — RFC 4648)
print(base32.decode_string(b32)) // expected: zolo
Challenge
Combine std::hash.sha256 with std::hex to display the fingerprint of a
string in the format sha256:<hex>, replicating the Docker image digest style.