A comprehensive cryptographic library for Mint with support for encryption, signing, hashing, and key derivation.
- Random Generation: Cryptographically secure bytes, UUIDs, integers
- Key Generation: AES, ECDSA, ECDH, RSA with multiple curves
- Key Derivation: PBKDF2, HKDF, ECDH key agreement
- Encryption: AES-GCM, AES-CBC, AES-CTR, RSA-OAEP
- Hashing: SHA-1, SHA-256, SHA-384, SHA-512, HMAC
- Encoding: Hex, Base64, Base64URL
- Signing: ECDSA, RSA-PSS, RSASSA
// Encrypt a string with a password case await Crypto.encrypt("Hello World", "password") { Ok(encrypted) => dbg "Encrypted successfully" Err(error) => dbg "Encryption failed" }
// Generate a key and encrypt data case await Crypto.generateKey { Err => dbg "Error" Ok(key) => case await Crypto.encryptWithKey("data", key) { Ok(encrypted) => dbg "Encrypted with key" Err => dbg "Error" } }
This library uses Array(Number) to represent binary data, where each number is a byte (0-255).
The Crypto.Encoding module provides functions to convert between bytes and various formats:
- Base64 encoding:
toBase64,fromBase64,toBase64Url,fromBase64Url - String conversion:
stringToBytes,bytesToString - Hex encoding:
toHex,fromHex
Most cryptographic functions come in two variants:
- Byte functions - Work directly with
Array(Number) - String functions - Accept strings and return strings
// Using bytes directly case await Crypto.Hash.digest([72, 101, 108, 108, 111], Crypto.HashAlgorithm.SHA256) { Ok(hashBytes) => dbg Crypto.Encoding.toHex(hashBytes) Err(error) => dbg "Error" } // Using string variant case await Crypto.Hash.digestStringHex("Hello", Crypto.HashAlgorithm.SHA256) { Ok(hex) => dbg hex // Same result as above Err(error) => dbg "Error" }
// String to bytes let bytes = Crypto.Encoding.stringToBytes("Hello") // [72, 101, 108, 108, 111] // Bytes to hex let hex = Crypto.Encoding.toHex(bytes) // "48656c6c6f" // Hex back to bytes case Crypto.Encoding.fromHex("48656c6c6f") { Ok(decoded) => dbg decoded // [72, 101, 108, 108, 111] Err(error) => dbg "Invalid hex" } // Bytes to base64 let base64 = Crypto.Encoding.toBase64(bytes) // "SGVsbG8="
Run all tests:
mint testAll cryptography uses the Web Crypto API built into modern browsers.