std/crypto
Cryptographic hashing and authenticated encryption, backed by the platform crypto library (CommonCrypto on macOS, OpenSSL on Linux).
from "std/crypto" import { Crypto }For dependency-free, constant-time implementations and higher-level primitives, see the pure-Milo companions: std/sha256, std/sha512, std/sha1, std/hmac, std/subtle, std/hkdf, std/pbkdf2, std/jwt, std/totp, and std/base32.
Example
let hash = Crypto.sha256("hello world")
// "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"AES-GCM is authenticated encryption with a 256-bit key (aesGcm*) or a 128-bit key (aesGcm128*). Decryption with the wrong key, IV or tag is an Err, never silent corruption:
let out = Crypto.aesGcmEncrypt(key, iv, "secret", "")!
let plain = Crypto.aesGcmDecrypt(key, iv, out.ciphertext, out.tag, "")!API reference
AesGcmResult
pub struct AesGcmResultThe two outputs of an AES-GCM encryption. Keep both: decryption needs the tag.
Fields: ciphertext: string, tag: string.
Crypto
pub struct CryptoCrypto namespace — Crypto.sha256/sha1/sha1Bytes/md5 (platform crypto lib) + AES-GCM.
Crypto.aesGcm128Decrypt
fn Crypto.aesGcm128Decrypt(key: &string, iv: &string, ciphertext: &string, tag: &string, aad: &string): Result<string, string>Decrypt and authenticate with AES-128-GCM: 16-byte key, 12-byte IV, 16-byte tag. A tag that does not verify is an Err.
Crypto.aesGcm128Encrypt
fn Crypto.aesGcm128Encrypt(key: &string, iv: &string, plaintext: &string, aad: &string): Result<AesGcmResult, string>Encrypt with AES-128-GCM. The key is 16 bytes and the IV is 12 bytes.
aad is additional authenticated data; pass "" if unused.
Crypto.aesGcmDecrypt
fn Crypto.aesGcmDecrypt(key: &string, iv: &string, ciphertext: &string, tag: &string, aad: &string): Result<string, string>Decrypt and authenticate with AES-256-GCM: 32-byte key, 12-byte IV, and the 16-byte tag from encryption. A tag that does not verify is an Err, never corrupted plaintext.
Crypto.aesGcmEncrypt
fn Crypto.aesGcmEncrypt(key: &string, iv: &string, plaintext: &string, aad: &string): Result<AesGcmResult, string>Encrypt with AES-256-GCM. The key is 32 bytes and the IV is 12 bytes.
aad is additional authenticated data; pass "" if unused. The key and IV lengths are preconditions (requires), not runtime errors.
Crypto.md5
fn Crypto.md5(input: &string): stringMD5 as a 32-char lowercase hex string.
Crypto.sha1
fn Crypto.sha1(input: &string): stringSHA-1 as a 40-char lowercase hex string.
Crypto.sha1Bytes
fn Crypto.sha1Bytes(input: &string): stringSHA-1 as the raw 20-byte digest (the WebSocket handshake, legacy protocols).
Crypto.sha256
fn Crypto.sha256(input: &string): stringSHA-256 as a 64-char lowercase hex string.