Crypto Wasm
    Preparing search index...

    Class WasmAccelerator

    WebAssembly cryptographic accelerator.

    When the WASM module is available, routes heavy operations through near-native WASM code. Falls back transparently to the JavaScript implementation when WASM is not loaded.

    Auto-detection: crypto-lib checks for this package at import time. If installed, heavy operations are automatically routed through WASM.

    import { WasmAccelerator } from "@sebastienrousseau/crypto-wasm";

    const accel = new WasmAccelerator();
    await accel.init();
    console.log(accel.isAvailable); // true if WASM loaded
    const digest = await accel.hash("sha256", new Uint8Array([1, 2, 3]));
    accel.destroy();
    Index
    • get isAvailable(): boolean

      Check if WASM acceleration is available.

      Returns boolean

    • get wasmModule(): Module | null

      The compiled WebAssembly module, or null if not loaded.

      Returns Module | null

    • Initialize the WASM module from a .wasm file or buffer.

      Parameters

      • OptionalwasmSource: BufferSource | Response

      Returns Promise<void>

    • Hash data with the WebAssembly module when one is loaded, otherwise with crypto-lib.

      Parameters

      • algorithm: string

        A crypto-lib hash name ("sha256", "sha384", "sha512", "sha3-256", "sha3-512", "blake2b", "blake3"); "SHA-256" and "sha-512" are read as "sha256" and "sha512".

      • data: Uint8Array

        Raw bytes to hash.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      The hash digest as a Uint8Array.

      For an algorithm crypto-lib does not support.

    • ML-KEM-768 post-quantum key encapsulation.

      Routes to WebAssembly implementation when available; falls back transparently to pure-JS implementation.

      Parameters

      • publicKey: Uint8Array

      Returns Promise<{ ciphertext: Uint8Array; sharedSecret: Uint8Array }>

    • ML-KEM-768 post-quantum key decapsulation.

      Routes to WebAssembly implementation when available; falls back transparently to pure-JS implementation.

      Parameters

      • secretKey: Uint8Array
      • ciphertext: Uint8Array

      Returns Promise<Uint8Array<ArrayBufferLike>>

    • ML-DSA-65 post-quantum digital signature generation.

      Routes to WebAssembly implementation when available; falls back transparently to pure-JS implementation.

      Parameters

      • secretKey: Uint8Array
      • message: Uint8Array

      Returns Promise<Uint8Array<ArrayBufferLike>>

    • ML-DSA-65 post-quantum digital signature verification.

      Routes to WebAssembly implementation when available; falls back transparently to pure-JS implementation.

      Parameters

      • publicKey: Uint8Array
      • message: Uint8Array
      • signature: Uint8Array

      Returns Promise<boolean>

    • Benchmark a hash operation: crypto-lib against this accelerator's WasmAccelerator.hash, which is crypto-lib too until a WebAssembly module is loaded.

      Parameters

      • operation: AcceleratedOperation

        "hash-sha256", "hash-sha512" or "hash-blake3".

      • iterations: number = 1000

        Number of iterations (default 1000).

      Returns Promise<BenchmarkResult>

      Benchmark results with timing and speedup ratio.

      For any other operation, which this method cannot time.

    • Reset the accelerator, releasing the WASM module.

      Returns void