module std.crypto
std.crypto — cryptography (II.1, §13.1). Every primitive is libcrypto's (OpenSSL 3 / AWS-LC through the EVP interface): this module only marshals buffers, it has no primitive of its own (TZ 3.4). Decisions D-3120..D-3129.
Hashes sha2, sha3, hmac, random bytes rand.secure, constantTimeEq, base64, authenticated encryption aead
(AES-256-GCM, ChaCha20-Poly1305), signatures ed25519, key agreement x25519, passwords password (scrypt) and
pbkdf2. Hex text is std.ser.hex / fromHex; the literal sha256"…" is a 32-byte bytes constant.
Not now: blake3, ecdsa, rsa, argon2 (libcrypto 3.0 has no Argon2), streaming digests.
Example:
use std.crypto { sha2, base64 }
use std.ser { bytesOf }
fn main() io {
let digest = sha2.sha256(bytesOf(&"abc"))
print(base64.encode(digest))
}Вывод
ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=
Типы
data aead | Authenticated encryption with a 32-byte key. |
data base64 | Base64 (RFC 4648) |
data CryptoError | An authenticated decryption failed |
data ed25519 | Ed25519 signatures (RFC 8032) |
data hmac | HMAC (RFC 2104) |
data KeyPair | A key pair; the private half is a secret<bytes> (never shown or logged, wiped when dropped). |
data password | Password hashing with scrypt (N = 2^15, r = 8, p = 1 by default). |
data rand | Cryptographically secure random numbers from the system generator (effect rand). |
data sha2 | SHA-2 digests: sha2.sha256(data) is 32 bytes, sha384 48, sha512 64. |
data sha3 | SHA-3 digests. |
data x25519 | X25519 key agreement (RFC 7748). |
Функции
constantTimeEq | Compares two byte strings in time that does not depend on where they differ (different lengths are unequal at once). |
pbkdf2 | PBKDF2-HMAC-SHA256 key derivation. |