@std/crypto.ms#
Import each required symbol with import Name from "@std/crypto.ms".
CryptoError Alternative#
Errors returned when cryptographic input is invalid or an algorithm is unavailable.
type CryptoError:
case InvalidInput
case Unsupported
case AuthenticationFailedimpl Error for CryptoError:
fn message() -> String:CryptoError.AuthenticationFailed#
AES-GCM authentication failed, including a wrong key, nonce, associated data or altered ciphertext.
case AuthenticationFailedCryptoError.InvalidInput#
Key/seed/nonce length, derivation iteration/count arguments or AES key size is invalid.
case InvalidInputCryptoError.Unsupported#
The HMAC String selector is not exactly sha256, sha384 or sha512.
case UnsupportedCryptoError.Error.message#
Returns a cryptography-prefixed diagnostic without exposing keys, plaintext or ciphertext.
fn message() -> String:HashAlgorithm Alternative#
Selects a supported SHA-2 algorithm without a runtime string spelling check. Existing hash functions and hmac(algorithm: String) remain available.
type HashAlgorithm:
case Sha256
case Sha384
case Sha512
fn digest(data: Bytes) -> Bytes:
fn hmac(key: Bytes, data: Bytes) -> Result[Bytes, CryptoError]:HashAlgorithm.Sha256#
SHA-256, producing a 32-byte digest or HMAC.
case Sha256HashAlgorithm.Sha384#
SHA-384, producing a 48-byte digest or HMAC.
case Sha384HashAlgorithm.Sha512#
SHA-512, producing a 64-byte digest or HMAC.
case Sha512HashAlgorithm.digest#
Computes this algorithm’s digest, checking cancellation between chunks.
fn digest(data: Bytes) -> Bytes:HashAlgorithm.hmac#
Computes HMAC with this algorithm and retains CryptoError results.
fn hmac(key: Bytes, data: Bytes) -> Result[Bytes, CryptoError]:constant_time_equal Function#
Compares byte strings without making the comparison time depend on their contents.
fn constant_time_equal(left: Bytes, right: Bytes) -> Bool:derive_key Function#
Derives a key with PBKDF2-HMAC-SHA-256 using the supplied iteration count and length.
fn derive_key(password: Bytes, salt: Bytes, iterations: Int, length: Int) -> Result[Bytes, CryptoError]:hmac Function#
Computes an HMAC using exactly sha256, sha384, or sha512 (case-sensitive).
Other algorithms return CryptoError.Unsupported.
fn hmac(key: Bytes, data: Bytes, algorithm: String) -> Result[Bytes, CryptoError]:open Function#
Authenticates and decrypts a value produced by seal using AES-GCM.
fn open(key: Bytes, nonce: Bytes, ciphertext: Bytes, associated_data: Bytes) -> Result[Bytes, CryptoError]:public_key Function#
Derives the Ed25519 public key corresponding to a 32-byte private seed.
fn public_key(seed: Bytes) -> Result[Bytes, CryptoError]:seal Function#
Encrypts and authenticates plaintext with AES-GCM. The caller supplies a unique nonce.
fn seal(key: Bytes, nonce: Bytes, plaintext: Bytes, associated_data: Bytes) -> Result[Bytes, CryptoError]:sha256 Function#
Returns the SHA-256 digest of the input.
fn sha256(data: Bytes) -> Bytes:sha384 Function#
Returns the SHA-384 digest of the input.
fn sha384(data: Bytes) -> Bytes:sha512 Function#
Returns the SHA-512 digest of the input.
fn sha512(data: Bytes) -> Bytes:sign Function#
Signs data with an Ed25519 private seed.
fn sign(seed: Bytes, data: Bytes) -> Result[Bytes, CryptoError]:verify Function#
Verifies an Ed25519 signature with a public key.
fn verify(key: Bytes, data: Bytes, signature: Bytes) -> Bool: