@adviser/cement - v0.0.0
    Preparing search index...

    Function toCryptoRuntime

    • Creates a CryptoRuntime abstraction with optional custom implementations.

      Provides a unified crypto interface that works across different JavaScript runtimes (browsers, Node.js, Deno, CF Workers). Automatically uses the global Web Crypto API when available, with fallback implementations that throw errors for unavailable operations. Allows overriding any method for testing or custom implementations.

      Parameters

      • cryptoOpts: Partial<CryptoRuntime> = {}

        Optional custom implementations for crypto operations

      Returns CryptoRuntime

      CryptoRuntime instance with crypto operations

      // Use default global crypto
      const crypto = toCryptoRuntime();
      const hash = await crypto.digestSHA256(new Uint8Array([1, 2, 3]));
      const randomData = crypto.randomBytes(16);

      // Override for testing
      const mockCrypto = toCryptoRuntime({
      randomBytes: (size) => new Uint8Array(size).fill(42),
      digestSHA256: async (data) => new ArrayBuffer(32)
      });

      // Encrypt/decrypt with AES-GCM
      const key = await crypto.importKey(
      'raw',
      new Uint8Array(32),
      { name: 'AES-GCM' },
      false,
      ['encrypt', 'decrypt']
      );