Skip to content

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.crypto
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),
}
  • base64_decode returns str — use base64_decode_bytes when the payload is binary (ciphertext, images).
  • hex_decode odd-length → .err.
  • URL encoding uses standard percent-encoding (space → %20, not +).
  • Pair crypto.sha256_bytes with base64_decode_bytes for bytes-input hashing.
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)
}

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).

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)
}
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)
}

20 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.crypto.argon2_hash argon2_hash(pw: str) -> str Argon2id PHC hash
std.crypto.argon2_verify argon2_verify(hash: str, pw: str) -> bool Verify (total, malformed → false)
std.crypto.bcrypt_hash bcrypt_hash(pw: str) -> str bcrypt hash
std.crypto.bcrypt_verify bcrypt_verify(hash: str, pw: str) -> bool Verify (total)
std.crypto.ct_eq ct_eq(a: str, b: str) -> bool Constant-time compare
std.crypto.ed25519_keypair ed25519_keypair() -> [str] [secret, public] parts
std.crypto.ed25519_sign ed25519_sign(sk: str, msg: str) -> Result<str, str> Sign (.err on bad key)
std.crypto.ed25519_verify ed25519_verify(pk: str, msg: str, sig: str) -> bool Verify (total)
std.crypto.hmac_sha256 hmac_sha256(key: str, msg: str) -> str HMAC-SHA256 lowercase hex
std.crypto.jwt_decode jwt_decode(token: str, secret: str) -> Result<str, str> Decode HS256
std.crypto.jwt_decode_ed jwt_decode_ed(token: str, pk: str) -> Result<str, str> Decode EdDSA
std.crypto.jwt_encode jwt_encode(payload: str, secret: str) -> str HS256 JWT
std.crypto.jwt_encode_ed jwt_encode_ed(payload: str, sk: str) -> Result<str, str> EdDSA JWT
std.crypto.random_bytes random_bytes(n: int) -> str Hex of n OS CSPRNG bytes
std.crypto.rsa_keypair rsa_keypair() -> Result<[str], str> RSA keypair
std.crypto.rsa_sign rsa_sign(sk: str, msg: str) -> Result<str, str> RSA sign
std.crypto.rsa_verify rsa_verify(pk: str, msg: str, sig: str) -> bool RSA verify (total)
std.crypto.sha256 sha256(s: str) -> str SHA-256 lowercase hex
std.crypto.sha256_bytes sha256_bytes(b: bytes) -> str SHA-256 over raw bytes
std.crypto.sha512 sha512(s: str) -> str SHA-512 lowercase hex
  • 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