A WebCrypto polyfill for Node.js that provides comprehensive cryptographic operations using standard Web Crypto API
npx @tessl/cli install tessl/npm-peculiar--webcrypto@1.5.0A comprehensive WebCrypto polyfill for Node.js that implements the standard Web Cryptography API using native Node.js crypto capabilities. This library provides full compatibility with browser WebCrypto while offering extensive algorithm support including advanced cryptographic operations not typically available in browsers.
npm install @peculiar/webcryptoimport { Crypto } from "@peculiar/webcrypto";For CommonJS:
const { Crypto } = require("@peculiar/webcrypto");import { Crypto } from "@peculiar/webcrypto";
const crypto = new Crypto();
// Generate a key pair
const keyPair = await crypto.subtle.generateKey(
{
name: "RSA-PSS",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"]
);
// Sign data
const data = new TextEncoder().encode("Hello, World!");
const signature = await crypto.subtle.sign(
{
name: "RSA-PSS",
saltLength: 32,
},
keyPair.privateKey,
data
);
// Generate random values
const randomBytes = new Uint8Array(32);
crypto.getRandomValues(randomBytes);
// Generate additional random values
const moreRandomBytes = new Uint8Array(16);
crypto.getRandomValues(moreRandomBytes);@peculiar/webcrypto is built on several key components:
subtle property and utility methodsMain Crypto class providing WebCrypto API compatibility with random value generation and UUID creation.
class Crypto implements globalThis.Crypto {
public subtle: SubtleCrypto;
getRandomValues<T extends ArrayBufferView | null>(array: T): T;
randomUUID(): `${string}-${string}-${string}-${string}-${string}`;
}AES encryption in multiple modes (CBC, CTR, GCM, ECB) with key wrapping capabilities, plus legacy DES support.
// AES key generation
interface AesKeyGenParams extends Algorithm {
name: "AES-CBC" | "AES-CTR" | "AES-GCM" | "AES-ECB" | "AES-KW" | "AES-CMAC";
length: 128 | 192 | 256;
}
// AES encryption parameters
interface AesGcmParams extends Algorithm {
name: "AES-GCM";
iv: BufferSource;
additionalData?: BufferSource;
tagLength?: 8 | 32 | 64 | 96 | 104 | 112 | 120 | 128;
}RSA operations (signatures, encryption, key wrapping) and Elliptic Curve cryptography (ECDSA, ECDH) with extensive curve support.
// RSA key generation
interface RsaHashedKeyGenParams extends RsaKeyGenParams {
name: "RSA-PSS" | "RSASSA-PKCS1-v1_5" | "RSA-OAEP";
hash: "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
}
// ECDSA key generation
interface EcKeyGenParams extends Algorithm {
name: "ECDSA" | "ECDH";
namedCurve: "P-256" | "P-384" | "P-521" | "K-256" | string;
}EdDSA signatures and Curve25519 key agreement for next-generation cryptographic applications (Node.js ≥14).
// EdDSA key generation
interface EdDsaKeyGenParams extends Algorithm {
name: "EdDSA";
namedCurve: "Ed25519" | "Ed448";
}
// ECDH-ES key generation
interface EcdhEsKeyGenParams extends Algorithm {
name: "ECDH-ES";
namedCurve: "X25519" | "X448";
}SHA family hash functions including SHA-3 variants and SHAKE extendable-output functions.
// Hash algorithms
type HashAlgorithm = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512" |
"SHA3-256" | "SHA3-384" | "SHA3-512" |
"shake128" | "shake256";
// SHAKE parameters
interface ShakeParams extends Algorithm {
name: "shake128" | "shake256";
length: number;
}PBKDF2 and HKDF key derivation functions with HMAC support for secure key management.
// PBKDF2 parameters
interface Pbkdf2Params extends Algorithm {
name: "PBKDF2";
salt: BufferSource;
iterations: number;
hash: "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
}
// HKDF parameters
interface HkdfParams extends Algorithm {
name: "HKDF";
hash: "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
salt: BufferSource;
info: BufferSource;
}