The WebCrypt class provides zero-dependency symmetric encryption using AES-256-GCM, streaming file encryption, WebRTC Insertable Streams (E2EE), LRU key caching, and HMAC helpers powered natively by the Web Crypto API.
- Instantiation
- Text Encryption
- Data Encryption
- Streaming File Encryption
- WebRTC Insertable Streams (E2EE)
- Key Caching & LRU Eviction
- HMAC & Key Derivation Helpers
- Static Constants
import { WebCrypt } from "webcrypt";
// Default instance
const wc = new WebCrypt();
// Custom key cache max size
const wcCustom = new WebCrypt({ maxCacheSize: 50 });Encrypts a plaintext string into a Base64-encoded payload containing salt, IV, and AES-256-GCM ciphertext.
- Parameters:
text(string): Plaintext string to encrypt.password(string): Secret password for key derivation.
- Returns:
Promise<string>— Base64 payload.
const encrypted = await wc.encryptText("Hello, WebCrypt!", "secret-password");Decrypts a Base64 payload back into plaintext string.
- Parameters:
b64(string): Base64-encoded encrypted payload.password(string): Decryption password.
- Returns:
Promise<string>— Decrypted string.
const decrypted = await wc.decryptText(encrypted, "secret-password");Serializes JavaScript objects or values to JSON and encrypts them.
- Parameters:
data(any): Any JSON-serializable JavaScript object or primitive.password(string): Encryption password.
- Returns:
Promise<string>— Base64 payload.
const payload = { userId: 42, role: "admin", sessionToken: "abc123xyz" };
const encrypted = await wc.encryptData(payload, "secret-password");Decrypts a Base64 payload and parses it back into a JavaScript object/value.
- Parameters:
b64(string): Base64 encrypted string.password(string): Decryption password.
- Returns:
Promise<any>— Parsed JavaScript value.
const data = await wc.decryptData(encrypted, "secret-password");
console.log(data.userId); // 42Encrypts a File or Blob in constant-memory 8MB chunks using windowed parallel processing.
- Parameters:
fileOrBlob(File|Blob): Target file/blob.password(string): Encryption password.options(Object, optional):parallelChunks(number): Number of parallel chunk promises (default: 4).
- Returns:
Promise<{ blob: Blob, filename: string }>
const { blob, filename } = await wc.encryptFile(userFile, "password", { parallelChunks: 4 });Decrypts a WebCrypt encrypted file or blob in chunks.
- Parameters:
fileOrBlob(File|Blob): Encrypted file/blob.password(string): Decryption password.options(Object, optional):parallelChunks(number): Parallel processing windows.
- Returns:
Promise<{ blob: Blob, filename: string }>
const { blob, filename } = await wc.decryptFile(encryptedBlob, "password");Creates an encryption transform function for RTCRtpScriptTransform / WebRTC Insertable Streams.
const transform = await wc.createEncryptTransform("conference-secret");
// Pass to RTCRtpSender.transformCreates a decryption transform function for RTCRtpReceiver.transform.
const transform = await wc.createDecryptTransform("conference-secret");
// Pass to RTCRtpReceiver.transformWebCrypt automatically caches derived PBKDF2 keys in memory using an LRU (Least Recently Used) cache to accelerate repeated encryptions while maintaining security.
// Clear key cache manually
wc.clearKeyCache();
// Stop background automatic cleanup interval
wc.stopAutoCleanup();Generates or derives a non-exportable HMAC CryptoKey.
const hmacKey = await wc.generateHmacKey("my-pass", "SHA-256");Derives a quantum-resistant HMAC key using iterative SHA-3 hashing.
const hmacKeySHA3 = await wc.generateHmacKeySHA3("my-pass", "SHA3-256", null, 10000);Computes and verifies HMAC authentication tags.
const tag = await wc.computeHmac("message", hmacKey);
const isValid = await wc.verifyHmac("message", tag, hmacKey);WebCrypt.ALGORITHM:"AES-GCM"WebCrypt.KEY_LENGTH:256WebCrypt.IV_LENGTH:12(96 bits)WebCrypt.SALT_LENGTH:16(128 bits)WebCrypt.PBKDF2_ITERATIONS:600,000WebCrypt.CHUNK_SIZE:8 * 1024 * 1024(8MB)