Sign inDemoInstall


Package Overview
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies



Minimal encryption/decryption JWE library.

Version published
Weekly downloads
decreased by-20.04%
Weekly downloads



6.0.0 - 2023-11-05


  • BREAKING: Require node >= 18.
  • BREAKING: Use P-256 curve elliptic keys for key agreement instead of X25519 when using the fips-compliant version.
  • Use @noble/curves to provide X25519 implementation. This lib is often used in other libs that are combined with this one and it has been through a comprehensive security audit. Additional benefits include speed and tree-shaking capabilities.



Minimal Cipher (@digitalbazaar/minimal-cipher)

Minimal encryption/decryption JWE library, secure algs only, browser-compatible.

Table of Contents




Every version of this library will only offer at most two algorithms for encryption/decryption: a recommended algorithm and a FIPS-compliant algorithm. The encryption API will expect the user to specify "recommended" or "fips" as the version of the algorithm to use, defaulting to "recommended".

In the event that the FIPS-compliant algorithm is the same as the recommended one in a given version of this library, then that particular version will use the same algorithm regardless of the user specified "version".

This version of the library will use "XChaCha20-Poly1305" as the "recommended" version and 256-bit "AES-GCM" as the FIPS-compliant version.

Note: XSalsa20-Poly1305 is an AE (Authenticated Encryption) algorithm, not an AEAD (Authenticated Encryption and Associated Data) algorithm, making it incompatible with the current requirements for a JWE (JOSE Web Encryption) protected clear text header.

This library's API requires an interface for Key Encryption Key (KEKs). This enables key material that is protected from exfiltration to be used via HSM/SSM APIs, including Web KMS (TODO: citation needed).


  • Node.js 14+ required.
  • Streams API required. Older browsers and Node.js <18 must use a polyfill.
  • Web Crypto API required. Older browsers and Node.js 14 must use a polyfill.

To install locally (for development):

git clone
cd minimal-cipher
npm install


Pick a Cipher interface (recommended or fips) and create an instance:

import {Cipher} from '@digitalbazaar/minimal-cipher';

const cipher = new Cipher(); // by default {version: 'recommended'}


To encrypt something (to create a cipher, serialized as a JWE JSON document), you will need:

  • Some data to encrypt (a string, an object, a stream)
  • Keys (called Key Agreement Keys, or KAKs for short)

(You'll also need a keyResolver, more about that later.)

First, assemble your Key Agreement public keys (you'll be encrypting with them, and the intended recipient will use the corresponding private keys to decrypt).

Put together a list of recipients (essentially, you're listing the ids of public/private key pairs that will be used to encrypt/decrypt the message):

// Retrieve them from config, a ledger, registry or back channel
const keyAgreementKey = await fetchFromSomewhere();

// or derive them from an existing Ed25519 signing key
import {X25519KeyAgreementKey2020} from '@digitalbazaar/x25519-key-agreement-key-2020';
import {Ed25519VerificationKey2020} from '@digitalbazaar/ed25519-verification-key-2020';
const keyPair = await Ed25519VerificationKey2020.generate();

const keyAgreementKey = X25519KeyPair.fromEd25519VerificationKey2020({keyPair});
// If the source key pair didn't have a controller set, don't forget to set one:
keyAgreementKey.controller = did; // The controller's DID = `${did}#${keyAgreementKey.fingerprint()}`;

// or derive them from an authentication key extracted from DID Document
const didDoc = await veresDriver.get({did});
const authnKey = didDoc.getVerificationMethod({proofPurpose: 'authentication'});
const edKeyPair = await Ed25519VerificationKey2020.from(authnKey);
const keyPair = X25519KeyPair.fromEd25519VerificationKey2020({keyPair});

const recipient = {
  header: {
    alg: 'ECDH-ES+A256KW'

const recipients = [recipient];

You'll also need a keyResolver. Notice that recipients lists only key IDs, not the keys themselves. A keyResolver is a function that accepts a key ID and resolves to the public key corresponding to it.

Some example resolvers:

// Basic hardcoded key resolver; you already have the key material
const publicKeyNode = {
  '@context': '',
  type: 'X25519KeyAgreementKey2020',
  publicKeyMultibase: keyAgreementKey.publicKeyMultibase
const keyResolver = async () => publicKeyNode;
// A more advanced resolver based on DID doc authentication keys
const keyResolver = async ({id}) => {
  // Use veres driver to fetch the authn key directly
  const keyPair = await Ed25519VerificationKey2020.from(await veresDriver.get({did: id}));
  // Convert authn key to key agreement key
  return X25519KeyPair.fromEd25519VerificationKey2020({keyPair});
// Using did-veres-one driver as a resolver for did:v1:nym: DID keys
// TODO: Implement this
// Using the did:key method driver as a key resolver

Create the JWE:

// To encrypt a string or a Uint8Array
const data = 'plain text';
const jweDoc = await cipher.encrypt({data, recipients, keyResolver});

// To encrypt an object
const obj = {key: 'value'};
const jweDoc = await cipher.encryptObject({obj, recipients, keyResolver});


Decrypt a JWE JSON Document, using a private keyAgreementKey:

const data = await cipher.decrypt({jwe, keyAgreementKey});

const object = await cipher.decryptObject({jwe, keyAgreementKey});

TODO: Describe the required KEK API: // id, algorithm, wrapKey({unwrappedKey}), and unwrapKey({wrappedKey})


See the contribute file!

PRs accepted.

If editing the README, please conform to the standard-readme specification.

Commercial Support

Commercial support for this library is available upon request from Digital Bazaar:


New BSD License (3-clause) © Digital Bazaar



Package last updated on 05 Nov 2023

Did you know?


Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap


Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc