@digitalbazaar/minimal-cipher

中文文档
6.1.1发布于 2026/06/05BSD-3-ClauseES Module近 30 天下载 2971

Minimal encryption/decryption JWE library.

原始使用文档
文档目录10 节
  1. Table of Contents
  2. Security
  3. Background
  4. Install
  5. Usage
  6. Encrypting
  7. Decrypting
  8. Contribute
  9. Commercial Support
  10. License

Minimal Cipher (@digitalbazaar/minimal-cipher)

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

Table of Contents

Security

TBD

Background

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).

Install

This software requires and supports maintained recent versions of Node.js and browsers. Updates may remove support for older unmaintained platform versions. Please use dependency version lock files and testing to ensure compatibility with this software.

To install from NPM:

https://www.npmjs.com/package/@digitalbazaar/minimal-cipher

npm install @digitalbazaar/minimal-cipher

To install locally (for development):

git clone https://github.com/digitalbazaar/minimal-cipher.git
cd minimal-cipher
npm install

Usage

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'}

Encrypting

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
keyAgreementKey.id = `${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: {
    kid: keyAgreementKey.id,
    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': 'https://w3id.org/security/suites/x25519-2020/v1',
  id: keyAgreementKey.id,
  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});

Decrypting

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})

Contribute

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: [email protected]

License

New BSD License (3-clause) © Digital Bazaar

版本与发布记录

@digitalbazaar/minimal-cipher 共发布 11 个版本,首次发布于 2021/03/13;过去 12 个月发布了 2 个版本。

版本发布日期距今
6.1.1latest2026/06/055 个月前
6.1.02026/06/055 个月前
6.0.02023/11/063 年前
5.1.12022/08/154 年前
5.1.02022/07/314 年前
5.0.02022/06/074 年前

关于 @digitalbazaar/minimal-cipher 的常见问题

@digitalbazaar/minimal-cipher 是什么?

Minimal encryption/decryption JWE library.

如何安装 @digitalbazaar/minimal-cipher?

在项目目录运行 npm install @digitalbazaar/minimal-cipher,或使用 pnpm add / yarn add / bun add。

@digitalbazaar/minimal-cipher 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/digitalbazaar__minimal-cipher 是否存在。

@digitalbazaar/minimal-cipher 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@digitalbazaar/minimal-cipher 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@digitalbazaar/minimal-cipher 可以商用吗?

@digitalbazaar/minimal-cipher 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@digitalbazaar/minimal-cipher 的下载量有多少?

根据 npm 官方下载统计,@digitalbazaar/minimal-cipher 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,971 次,日均约 99 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digitalbazaar/minimal-cipher 还在维护吗?

最新版本 6.1.1 发布于 2026/06/05(5 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 2 个;首次发布于 2021/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。