# @cubbit/enigma 中文文档

规范地址：https://npmcn.com/package/%40cubbit/enigma/

版本：1\.3\.1
npm 元数据更新时间：2022-08-26T11:38:23.970Z
中文译文更新时间：2026-11-11T04:07:09.333Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cubbit/enigma
原文对照：https://npmcn.com/package/%40cubbit/enigma/?lang=original
项目主页：https://github.com/cubbit/enigma#readme
源码仓库：https://github.com/cubbit/enigma

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：2.7 MB，63 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=10
- 运行依赖：7 个
- 首次发布：2019-03-13T16:44:32.553Z
- 最新版本发布：2022-08-26T11:38:23.814Z
- 近 30 天下载量：4886（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：17（近 12 个月 0 个）

## 中文简介

一个快速的,本地的,加密的网络引擎

## 安装

```sh
npm install @cubbit/enigma
```

也可以使用：`pnpm add @cubbit/enigma`、`yarn add @cubbit/enigma`、`bun add @cubbit/enigma`

## README

# 谜题 [![Build Status](https://travis-ci.org/cubbit/enigma.svg?branch=master)](https://travis-ci.org/cubbit/enigma)

快速的,本地的,环境不可知的,密码的网络引擎

```ts
import Enigma from '@cubbit/enigma';

new Enigma.AES().init().then(async (aes: Enigma.AES) =>
{
    const my_secret = 'My secret';

    const cipher = await aes.encrypt(my_secret);
    console.log(cipher);
});

/*
{
    content: <Buffer 16 6e b6 61 1b e0 d9 3a 25>,
    tag: <Buffer 07 cf 9d 9c 53 6a 13 5f 5f 24 75 a3 64 1f bd 89>,
    iv: <Buffer 62 d6 9b 8c bc 23 3c e5 9b 77 30 2e 56 cc f9 35>
}
*/
```

Enigma是一个加密库,既可用于Node.js平台,也可用于Web. 它依靠OpenSSL提供最常用的密码公用事业. 在网络环境中,Enigma利用 [网络大会](https://webassembly.org/)\- 编译了OpenSSL版本来提升性能.

## 安装

Enigma 是一个 npm 模块,可以通过 [npm 登记](https://www.npmjs.com/)。 。 。 . 在节点.js和网络环境中使用 `npm install` 命令 :

```bash
npm install @cubbit/enigma
```

如果您想要从源头工作,只需复制回波并运行安装脚本如下:

```bash
git clone https://github.com/cubbit/enigma.git
cd enigma
npm install
```

## 节点.js

在安装前, [下载并安装节点.js](https://nodejs.org/en/download/). Node.js版本需要8.0或更高(Node.js 11尚未测试).

以下平台支持Enigma.

|  | 编号x86 | x64 (韩语) | 手臂32 | 军火64 |
| --- | --- | --- | --- | --- |
| 链接 | ︎︎︎ | ✔︎ | ✔︎ | ✔︎ |
| 马科斯 | \- 说吧 | ✔︎ | \- 说吧 | ✔︎ |
| 窗口 | ✔︎ | ✔︎ | \- 说吧 | \- 说吧 |

安装后仅导入 `@cubbit/enigma` 在你的代码,你准备去.

## 网页

通过跟踪程序安装库 [安装](https://npmcn.com/package/%40cubbit/enigma/#readme-Installation) 节。 然后,只是导入 `@cubbit/enigma` 在你的源头 并使用它,就像你会做 在节点.js.

**重要**: Enigma 需要一个缓冲多填充器才能在网络上正确工作. 默认由 [网络软件包](https://webpack.js.org/) 还好。 否则你需要自己提供.

## 特征

谜语包括以下加密设施:

-   **吸积算法( H)[SHA256 导弹](https://wikipedia.org/wiki/Secure_Hash_Algorithm)(中文(简体) )**
-   **缩略语加密算法( E)[AES256 法国](https://wikipedia.org/wiki/Advanced_Encryption_Standard)(中文(简体) )**
-   **非对称加密算法( E)[登记册系统管理人](https://en.wikipedia.org/wiki/RSA_\(cryptosystem\)), (中文(简体) ), [幼儿保育中心](https://en.wikipedia.org/wiki/Elliptic-curve_cryptography)(中文(简体) )**
-   **杂项公用事业(Diffie Hellman钥匙交换、Random、, [密钥衍生](https://en.wikipedia.org/wiki/Key_derivation_function) 算法)**

请参看 [API 密码](https://npmcn.com/package/%40cubbit/enigma/#readme-API) 以获取更多关于如何使用每个系统的信息

## 实例

### 欢呼

```ts
import Enigma from '@cubbit/enigma';

const message = 'Hello world';
const hash = Enigma.Hash.digest(message);

console.log(hash); // A591A6D40BF420404A011733CFB7B190D62C65BF0BCDA32B57B277D9AD9F146E
```

### 用 AES 加密

```ts
import Enigma from '@cubbit/enigma';

new Enigma.AES().init().then(async (aes: Enigma.AES) =>
{
    const my_secret = 'My secret';

    const cipher = await aes.encrypt(my_secret);
    console.log(cipher);
});

/*
{
    content: <Buffer 16 6e b6 61 1b e0 d9 3a 25>,
    tag: <Buffer 07 cf 9d 9c 53 6a 13 5f 5f 24 75 a3 64 1f bd 89>,
    iv: <Buffer 62 d6 9b 8c bc 23 3c e5 9b 77 30 2e 56 cc f9 35>
}
*/
```

### 使用 AES 流加密文件

当加密一个大文件时,可能会遇到浏览器限制或内存问题. AES流派班的设计是为了克服这些问题.

```ts
// On Node.js
import {createReadStream} from 'fs';
import Enigma from '@cubbit/enigma';

const file_stream = fs.createReadStream('my_secret_image.png');
new Enigma.AES().init().then((aes: Enigma.AES) =>
{
    const iv = Enigma.Random.bytes(16);
    const aes_stream = aes.encrypt_stream(iv);

    aes_stream.once('finish', () => console.log('File encrypted'));

    file_stream.pipe(aes_stream);
});

// On the Web
import Enigma from '@cubbit/enigma';
import WebFileStream from '@cubbit/web-file-stream';

const file = new File(); // You can get this File object through an file input tag
const file_stream = WebFileStream.create_read_stream(file);

new Enigma.AES().init().then((aes: Enigma.AES) =>
{
    const iv = Enigma.Random.bytes(16);
    const aes_stream = aes.encrypt_stream(iv);

    aes_stream.once('finish', () => console.log('File encrypted'));

    file_stream.pipe(aes_stream);
});
```

### 用 AES 解密

```ts
import Enigma from '@cubbit/enigma';

const existing_key = /*...*/
const aes = new Enigma.AES().init({key: existing_key}).then(async (aes: Enigma.AES =>
{
    const message = aes.decrypt(my_secret).toString();
    console.log(message); // "My secret"
});

```

### 生成 RSA 密钥派

```ts
import Enigma from '@cubbit/enigma';

const keypair = Enigma.RSA.create_keypair();
```

### 用 RSA 加密和解密

```ts
import Enigma from  '@cubbit/enigma';

const message = 'My secret';
new Enigma.RSA().init().then(async (rsa: Enigma.RSA) =>
{
    const encrypted = await Enigma.RSA.encrypt(message, rsa.keypair.public_key);
    console.log(encrypted);

    /*
    <Buffer 7c 01 29 9e 8e 8a 5c a0 ad 28 5a 19 b4 97 43 96 ca 49 0f 73 f9 bf 4d 27 7a 01 c7 d8 11 b5 8f c4 1e 69 c1 cc ef a2 74 03 8f 04 bc 0e 3d c2 4d 89 c4 10 ... >
    */

    const decrypted = (await rsa.decrypt(encrypted)).toString();
    console.log(decrypted); // "My secret"
});
```

### 生成 ECC 密钥派

```ts
import Enigma from '@cubbit/enigma';

const keypair = Enigma.ED25519.create_keypair();
```

### 与 ECC 签署和核实信件

```ts
import Enigma from '@cubbit/enigma';

const message = 'To be signed';
const ecc = new Enigma.ED25519();
const signature = ecc.sign(message);

Enigma.ED25519.verify(message, ecc.keypair.public_key, signature).then(console.log) // true
```

### 用 pbkdf2 执行密钥导出

```ts
import Enigma from '@cubbit/enigma';

const message = 'Original message';
const salted_key = await Enigma.KeyDerivation.pbkdf2(message);
```

### 用律师工具签名 Javascript 对象

```ts
import Enigma from '@cubbit/enigma';

const object = {message: 'To be signed'};
const ecc = new Enigma.ED25519();

const contract = Enigma.Attorney.redact(object, ecc);
const is_valid = Enigma.Attorney.verify(contract, ecc.keypair.public_key);

console.log(is_valid); // true
```

### 生成随机值

```ts
import Enigma from '@cubbit/enigma';

Enigma.init().then(async () =>
{
    const random_int4 = Enigma.Random.integer(32);
    const random_bytes = Enigma.Random.bytes(32);
});
```

### 迪菲-地狱人密钥交换

允许基于椭圆曲线的 Diffie Hellman 键变的类 . 采用椭圆曲线 _(原始内容存档于2019-09-62) (英语). NID 9 62 prime256v1_。 。 。 .

-   `initialize(): void`:生成密钥对.
-   `get_public_key(): string`: 返回公钥作为具有这些属性的字符串: _总统_ 格式; 未压缩; ASN.1 标准格式 _命名的法院_。 。 。 .
-   `derive_secret(endpoint_public_key: string): string`:需要使用上述相同格式的公用密钥,并以六分位格式返回作为字符串的密钥.

```ts
import Enigma from '@cubbit/enigma';

Enigma.init().then(async () =>
{
    const dh = new Enigma.DiffieHellman();

    dh.initialize();

    const public_key: string = dh.get_public_key();

    // receive public key from remote endpoint
    // send my public key to remote endpoint

    const shared_secret: string = await dh.derive_secret(endpoint_public_key);
});
```

## 如何重建束缚

为了构建工程的捆绑,只需在复制寄存器后运行以下命令:

```bash
npm run build
npm run build:web
```

### 先决条件

-   [百分数](http://strawberryperl.com/) 需要在窗口上构建 OpenSSL
-   [插头](https://www.docker.com/) 网络建设所需

## 如何运行测试

要运行测试套件,首先安装依赖,然后运行 `npm test`数字 :

```bash
npm install
npm test
```

## 如何捐款

随意打开一个问题或拉取请求来报告错误并推荐新的特性. 请参看我们 [捐款准则](https://github.com/cubbit/enigma/blob/master/CONTRIBUTING.md) 详细介绍缴款过程.

## 许可证

[麻省理工学院](https://github.com/cubbit/enigma/blob/master/LICENSE)

## 常见问题

### @cubbit/enigma 是什么？

一个快速的,本地的,加密的网络引擎

### 如何安装 @cubbit/enigma？

在项目目录运行 npm install @cubbit/enigma，或使用 pnpm add / yarn add / bun add。

### @cubbit/enigma 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @cubbit/enigma 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @cubbit/enigma 需要什么版本的 Node\.js？

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

### @cubbit/enigma 可以商用吗？

@cubbit/enigma 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @cubbit/enigma 的下载量有多少？

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

### @cubbit/enigma 还在维护吗？

最新版本 1\.3\.1 发布于 2022/08/26（4 年前）；至今共发布 17 个版本，其中过去 12 个月发布了 0 个；首次发布于 2019/03/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

