OxL Справочник OxL
РусскийEnglish

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:

oxl✓ проверяется в CI
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 aeadAuthenticated encryption with a 32-byte key.
data base64Base64 (RFC 4648)
data CryptoErrorAn authenticated decryption failed
data ed25519Ed25519 signatures (RFC 8032)
data hmacHMAC (RFC 2104)
data KeyPairA key pair; the private half is a secret<bytes> (never shown or logged, wiped when dropped).
data passwordPassword hashing with scrypt (N = 2^15, r = 8, p = 1 by default).
data randCryptographically secure random numbers from the system generator (effect rand).
data sha2SHA-2 digests: sha2.sha256(data) is 32 bytes, sha384 48, sha512 64.
data sha3SHA-3 digests.
data x25519X25519 key agreement (RFC 7748).

Функции

constantTimeEqCompares two byte strings in time that does not depend on where they differ (different lengths are unequal at once).
pbkdf2PBKDF2-HMAC-SHA256 key derivation.

См. также