Skip to content
xudongccPublic

About

Zero-dependency synchronous and asynchronous RFC 7516 JWE (Compact, Flattened, General) powered by node:crypto

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

node-jwe

Zero-dependency synchronous and asynchronous RFC 7516 JSON Web Encryption (JWE) for Node.js — supporting Compact, Flattened, and General serializations — powered natively by node:crypto and node:zlib with 1:1 jose JWE API compatibility.

Features

  • 1:1 jose JWE compatibility:
    • Compact JWE: CompactEncrypt, compactDecrypt, compactDecryptSync
    • Flattened JWE: FlattenedEncrypt, flattenedDecrypt, flattenedDecryptSync
    • General JWE (multi-recipient): GeneralEncrypt, generalDecrypt, generalDecryptSync
    • Utilities: decodeProtectedHeader, base64url, and errors
  • Synchronous & asynchronous APIs: .encryptSync() and *DecryptSync() alongside .encrypt() and *Decrypt(), ideal for synchronous ORM custom column types, serializers, and request pipelines.
  • Zero runtime dependencies: Built exclusively on Node.js node:crypto and node:zlib.
  • Full RFC 7518 JWE algorithm matrix:
    • alg: dir, A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW, A256GCMKW, RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512, ECDH-ES, ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KW, PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW
    • enc: A128GCM, A192GCM, A256GCM, A128CBC-HS256, A192CBC-HS384, A256CBC-HS512
    • zip: DEF (raw DEFLATE with configurable maxDecompressedLength bomb protection)

Installation

pnpm add node-jwe
# or
npm install node-jwe

Requires Node.js >= 24.0.0.

Usage

Compact JWE (Synchronous)

import { randomBytes } from "node:crypto";
import { CompactEncrypt, compactDecryptSync } from "node-jwe";

const secret = randomBytes(32);

const jwe = new CompactEncrypt(new TextEncoder().encode("hello world"))
  .setProtectedHeader({ alg: "dir", enc: "A256GCM" })
  .encryptSync(secret);

const { plaintext, protectedHeader } = compactDecryptSync(jwe, secret, {
  keyManagementAlgorithms: ["dir"],
  contentEncryptionAlgorithms: ["A256GCM"],
});

Flattened JWE (with Additional Authenticated Data)

import { generateKeyPairSync } from "node:crypto";
import { FlattenedEncrypt, flattenedDecryptSync } from "node-jwe";

const { publicKey, privateKey } = generateKeyPairSync("rsa", {
  modulusLength: 2048,
});

const jwe = new FlattenedEncrypt(new TextEncoder().encode("payload"))
  .setProtectedHeader({ alg: "RSA-OAEP-256", enc: "A256GCM" })
  .setAdditionalAuthenticatedData(new TextEncoder().encode("context-aad"))
  .encryptSync(publicKey);

const { plaintext, additionalAuthenticatedData } = flattenedDecryptSync(
  jwe,
  privateKey,
);

General JWE (Multi-Recipient)

import { generateKeyPairSync, randomBytes } from "node:crypto";
import { GeneralEncrypt, generalDecrypt } from "node-jwe";

const ec = generateKeyPairSync("ec", { namedCurve: "P-256" });
const kek = randomBytes(16);

const jwe = await new GeneralEncrypt(new TextEncoder().encode("shared secret"))
  .setProtectedHeader({ enc: "A256GCM" })
  .addRecipient(ec.publicKey)
  .setUnprotectedHeader({ alg: "ECDH-ES+A256KW", kid: "ec-1" })
  .addRecipient(kek)
  .setUnprotectedHeader({ alg: "A128KW", kid: "kek-1" })
  .encrypt();

const { plaintext, unprotectedHeader } = await generalDecrypt(jwe, kek);

License

MIT

About

Zero-dependency synchronous and asynchronous RFC 7516 JWE (Compact, Flattened, General) powered by node:crypto

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages