Extensions to the Web Crypto API supporting additional encryption APIs, but also delegating to the built-in APIs when possible.
Provides additional digest algorithms that are not part of the WebCrypto
standard as well as a subtle.digest and subtle.digestSync methods.
The KeyStack export implements the KeyRing interface
for managing rotatable keys for signing data to prevent tampering, like with
HTTP cookies.
Supported algorithms
Here is a list of supported algorithms. If the algorithm name in WebCrypto and Wasm/Rust is the same, this library prefers to use algorithms that are supported by WebCrypto.
WebCrypto:
SHA-384SHA-256SHA-512(length-extendable and collidable)
Wasm/Rust:
BLAKE2B-128BLAKE2B-160BLAKE2B-224BLAKE2B-256BLAKE2B-384BLAKE2BBLAKE2SBLAKE3FNV32(length-extendable)FNV32A(length-extendable)FNV64(length-extendable)FNV64A(length-extendable)KECCAK-224KECCAK-256KECCAK-384KECCAK-512SHA-384SHA3-224SHA3-256SHA3-384SHA3-512SHAKE128SHAKE256TIGERRIPEMD-160(length-extendable)SHA-224(length-extendable)SHA-256(length-extendable)SHA-512(length-extendable)MD4(collidable and length-extendable)MD5(collidable and length-extendable)SHA-1(collidable and length-extendable)
Examples
Example 1
Example 1
import { crypto } from "@std/crypto"; // This will delegate to the runtime's WebCrypto implementation. console.log( new Uint8Array( await crypto.subtle.digest( "SHA-384", new TextEncoder().encode("hello world"), ), ), ); // This will use a bundled Wasm/Rust implementation. console.log( new Uint8Array( await crypto.subtle.digest( "BLAKE3", new TextEncoder().encode("hello world"), ), ), );
Convert hash to a string
Convert hash to a string
import { crypto, } from "@std/crypto"; import { encodeHex } from "@std/encoding/hex" import { encodeBase64 } from "@std/encoding/base64" const hash = await crypto.subtle.digest( "SHA-384", new TextEncoder().encode("You hear that Mr. Anderson?"), ); // Hex encoding console.log(encodeHex(hash)); // Or with base64 encoding console.log(encodeBase64(hash));
Interfaces
- subtle: StdSubtleCrypto
Extension to the
crypto.SubtleCryptointerface.
Extensions to the web standard SubtleCrypto interface.
- digest(): Promise<ArrayBuffer>algorithm: DigestAlgorithm,data:BufferSource
| AsyncIterable<BufferSource>
| Iterable<BufferSource>Returns a new
Promiseobject that will digestdatausing the specifiedAlgorithmIdentifier. - digestSync(): ArrayBufferalgorithm: DigestAlgorithm,data: BufferSource | Iterable<BufferSource>
Returns a ArrayBuffer with the result of digesting
datausing the specifiedAlgorithmIdentifier.
Type Aliases
Extended digest algorithms accepted by stdCrypto.subtle.digest.
Extended digest algorithm names.
Extended digest algorithm objects.
FNV (Fowler/Noll/Vo) algorithms names.
An algorithm name supported by std/crypto/_wasm.
Variables
An wrapper for WebCrypto adding support for additional non-standard algorithms, but delegating to the runtime WebCrypto implementation whenever possible.
| "BLAKE2B-160"
| "BLAKE2B-224"
| "BLAKE2B-256"
| "BLAKE2B-384"
| "BLAKE2B"
| "BLAKE2S"
| "BLAKE3"
| "KECCAK-224"
| "KECCAK-256"
| "KECCAK-384"
| "KECCAK-512"
| "SHA-384"
| "SHA3-224"
| "SHA3-256"
| "SHA3-384"
| "SHA3-512"
| "SHAKE128"
| "SHAKE256"
| "TIGER"
| "RIPEMD-160"
| "SHA-224"
| "SHA-256"
| "SHA-512"
| "MD4"
| "MD5"
| "SHA-1"
All cryptographic hash/digest algorithms supported by std/crypto/_wasm.