Cryptographic algorithms for MoonBit, one per package.
let sha256 = fn() -> &@spec.Hash { @sha2.Hasher::new() }
// A digest, a MAC, and a key derived from a password.
let digest = @sha2.hash(b"the quick brown fox"[:])
let tag = @hmac.mac(b"shared secret"[:], b"the quick brown fox"[:], sha256)
let key = @pbkdf2.key(b"correct horse"[:], b"battery staple"[:], sha256,
rounds=600000, len=32)
// Authenticated encryption, over any 128-bit block cipher.
let box = @sm4gcm.new(key[0:16])
let sealed = box.seal(nonce=nonce[:], plain=b"the quick brown fox"[:], aad=b"v1"[:])
box.open(nonce=nonce[:], cipher=sealed[:], aad=b"v1"[:]) // the plaintext, or a raise
// A signature. Ed25519 signing is deterministic: the same key and message
// always give the same bytes. A fresh key takes its randomness from a source
// the caller hands over, here the platform's, from moonrand.
let sk = @ed25519.PrivateKey::generate(@system.source)
let sig = sk.sign(b"the quick brown fox"[:])
sk.public().verify(b"the quick brown fox"[:], sig[:]) // true| Package | What | Specification |
|---|---|---|
| spec | The Hash, Mac, Block, Aead, Signer and Verifier traits, constant-time comparison, erasure | — |
| hash/sha2 | SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256 | FIPS 180-4 |
| hash/sha1 | SHA-1 — legacy: collisions are cheap enough to be routine | FIPS 180-4 |
| hash/sha3 | SHA3-224/256/384/512, SHAKE128 and SHAKE256, and the cSHAKE encoding rules KMAC is built from | FIPS 202, SP 800-185 |
| hash/blake2 | BLAKE2b and BLAKE2s, with key, salt and personalisation | RFC 7693 |
| hash/blake3 | BLAKE3, including keyed and derive-key modes and the extendable output | BLAKE3 spec |
| hash/sm3 | SM3 | GB/T 32905-2016 |
| hash/md5 | MD5 — legacy, and broken for anything adversarial | RFC 1321 |
| hash/ripemd | RIPEMD-160 — legacy: a 1996 design kept for the data that already uses it | Dobbertin–Bosselaers–Preneel |
| mac/hmac | HMAC over any Hash | RFC 2104, FIPS 198-1 |
| mac/poly1305 | Poly1305, the one-time authenticator | RFC 8439 §2.5 |
| mac/cmac | CMAC over any Block, any tag length up to the block size | SP 800-38B |
| mac/gmac | GMAC, GCM's authenticator on its own | SP 800-38D |
| mac/kmac | KMAC128 and KMAC256, and the XOF form | SP 800-185 §4 |
| mac/siphash | SipHash-2-4, a 64-bit keyed hash | Aumasson & Bernstein 2012 |
| sum/crc32 | CRC-32 and CRC-32C | IEEE 802.3, RFC 3720 |
| sum/crc16 | The common CRC-16 variants | CRC catalogue |
| sum/adler32 | Adler-32 | RFC 1950 §9 |
| sum/fnv | FNV-1 and FNV-1a, 32 and 64 bit | Fowler–Noll–Vo |
| sum/xxhash | XXH32 and XXH64, with a seed | xxHash spec |
| sum/murmur3 | MurmurHash3 x86_32 and x64_128 | Appleby |
| kdf/hkdf | HKDF, extract and expand separately | RFC 5869 |
| kdf/pbkdf2 | PBKDF2 | RFC 8018 §5.2 |
| kdf/scrypt | scrypt, with presets for interactive and sensitive use, and the parameter check | RFC 7914 |
| kdf/argon2 | Argon2d, Argon2i and Argon2id, with secret and associated data | RFC 9106 |
| kdf/bcrypt | bcrypt, the $2b$ text format and a constant-time verify | Provos & Mazières 1999 |
| kdf/concat | The Concat KDF, as a NIST key agreement derives from a shared secret | SP 800-56A §5.8.1 |
| curve | The curves an EC key can live on — P-256, P-384, P-521, secp256k1 — and their coordinate width; sign/ecdsa and kex/ecdh re-export it, import it to name a curve | FIPS 186-4, SEC 2 |
| sign/ed25519 | Ed25519 | RFC 8032 |
| sign/ed448 | Ed448, with context and the prehash form | RFC 8032 |
| sign/ecdsa | ECDSA over P-256, P-384, P-521 and secp256k1, with RFC 6979 nonces | FIPS 186-4, RFC 6979 |
| sign/schnorr | Schnorr over secp256k1, x-only keys and all | BIP 340 |
| sign/sm2 | SM2, with the signer identity mixed into the hash | GB/T 32918.2-2016 |
| rsa | RSA PKCS#1 v1.5 and PSS signatures, OAEP encryption | RFC 8017 |
| cipher/aes | AES-128, AES-192 and AES-256, both directions | FIPS 197 |
| cipher/sm4 | SM4 | GB/T 32907-2016 |
| cipher/camellia | Camellia-128/192/256 | RFC 3713 |
| cipher/mode | CBC and CTR over any Block, with PKCS#7 padding | SP 800-38A |
| cipher/chacha20 | ChaCha20 with the 96-bit nonce, and one block on its own for QUIC's header protection | RFC 8439 §2.3–2.4 |
| cipher/xchacha20 | XChaCha20, the 192-bit nonce form | draft-irtf-cfrg-xchacha |
| cipher/blowfish | Blowfish — legacy, and the core of bcrypt | Schneier 1993 |
| cipher/des | DES and triple DES, E-D-E in both key forms — legacy: a 56-bit key | FIPS 46-3 |
| cipher/rc4 | RC4 and its keystream — legacy: the first bytes are biased | — |
| cipher/salsa20 | Salsa20 — legacy: superseded by ChaCha20 | Bernstein 2005 |
| aead/gcm | GCM over any 128-bit block cipher, any nonce length, the seven tag lengths | SP 800-38D |
| aead/chachapoly | ChaCha20-Poly1305 | RFC 8439 §2.8 |
| aead/sm4gcm | SM4-GCM | SP 800-38D, GB/T 32907 |
| aead/ccm | CCM over any 128-bit block cipher, any even tag length from 4 to 16 bytes | SP 800-38C, RFC 3610 |
| aead/siv | AES-SIV, nonce misuse resistant, with the multi-AD form | RFC 5297 |
| kex/x25519 | X25519, with a low-order point refused | RFC 7748 |
| kex/x448 | X448, with the same refusal | RFC 7748 |
| kex/ecdh | ECDH over P-256, P-384, P-521 and secp256k1, with an off-curve point refused | SEC 1 §3.3.1 |
| kex/dh | Finite-field Diffie-Hellman over the five ffdhe groups, with a range check on the peer's share | RFC 7919 |
| otp | HOTP and TOTP, 6 to 9 digits, with a clock skew window | RFC 4226, RFC 6238 |
| key | Key files: PEM around DER, in PKCS#1, PKCS#8, SPKI and SEC1 shapes | RFC 7468, RFC 8017, RFC 5958, RFC 5915 |
| asn1 | ASN.1 DER, read and write — the layer a key file and a certificate are built on | ITU-T X.690 |
| Setting | Why there is no default |
|---|---|
| pbkdf2.key(rounds~) | The iteration count is the whole security argument and goes out of date silently. Python's hashlib.pbkdf2_hmac, Go's pbkdf2.Key and Node's crypto.pbkdf2 all require it, so requiring it is the mainstream default |
| len~ on HKDF and PBKDF2 | How many bytes of key are wanted is a property of what the key is for |
| gcm.Gcm::new(tag~) | 16 — the full 128-bit tag, the only length SP 800-38D appendix C leaves unqualified. The shorter ones it defines are there, and each costs forgery resistance the appendix works out |
moon add moonbitstack/mooncryptInstall
Download zipCryptographic algorithms for MoonBit, one per package.