std.crypto - ZZ
Digests (lowercase hex), HMAC, CSPRNG, constant-time compare, password hashing (Argon2id + bcrypt), and asymmetric signatures/JWT (ed25519 / RSA). All digests are lowercase hex strings.
import std.cryptoProduction 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),}Gotchas
Section titled “Gotchas”base64_decodereturnsstr— usebase64_decode_byteswhen the payload is binary (ciphertext, images).hex_decodeodd-length →.err.- URL encoding uses standard percent-encoding (space →
%20, not+). - Pair
crypto.sha256_byteswithbase64_decode_bytesfor bytes-input hashing.
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)}Purpose
Section titled “Purpose”std.crypto covers hashing, MACs, random bytes, password hashing, public-key signatures, and JWT. Digests return lowercase hex; random_bytes returns hex of n CSPRNG bytes. Compare secrets with ct_eq, never ==. Password hashes are PHC strings; verification is total (malformed input verifies as false).
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)}Production patterns
Section titled “Production patterns”import std.crypto
func main() -> Result<int, str> { // Constant-time API key check expected := "sk_live_..." client_key := "sk_live_..." if crypto.ct_eq(client_key, expected) { println("authorized") }
// Password reset token token := crypto.jwt_encode(`{"uid":123}`, "reset-secret") println(token) return .ok(0)}API reference
Section titled “API reference”20 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.encoding— Codecs: base64, hex, URL encoding and JSON escaping.std.uuid— UUID v4 and v7 generation, parsing and validation.
Full index: API reference · Standard library