IronCoreLabs/recrypt-wasm-binding
GitHub: IronCoreLabs/recrypt-wasm-binding
将 Recrypt Rust 库编译为 WebAssembly,实现在浏览器中运行端到端加密。
Stars: 22 | Forks: 3
# Recrypt WebAssembly 绑定
[](https://travis-ci.org/IronCoreLabs/recrypt-wasm-binding)
[](https://www.npmjs.com/package/@ironcorelabs/recrypt-wasm-binding)
此仓库包含允许 [Recrypt](<(https://github.com/IronCoreLabs/recrypt-rs) 在浏览器中作为 WebAssembly 模块使用的绑定。它依赖于 [recrypt-rs](https://github.com/IronCoreLabs/recrypt-rs) 作为依赖项,并包含用于在 WebAssembly 和 JavaScript 之间编组数据的 Rust 和 JS 存根。这些绑定使用 [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) 生成。
## 安装
```
npm install @ironcorelabs/recrypt-wasm-binding
```
## 示例
以下示例展示了如何从基于浏览器的 Web 应用程序中使用此库。为了正确处理 ES6 模块的导入并正确加载和实例化 WebAssembly 模块,此库需要通过 [webpack](https://webpack.js.org) 之类的模块捆绑器加载。如果通过 webpack 使用,该模块还需要异步加载。请参考 [webpack.config.js](webpack.config.js) 文件,其中展示了如何为此模块加载用于基准测试和单元测试的代码,这两者均在浏览器中运行。
### 基本加密/解密示例
```
import * as Recrypt from "@ironcorelabs/recrypt-wasm-binding";
//Create a new Recrypt API instance
const Api256 = new Recrypt.Api256();
//Generate both a user key pair and a signing key pair
const keys = Api256.generateKeyPair();
const signingKeys = Api256.generateEd25519KeyPair();
//Generate a plaintext to encrypt
const plaintext = Api256.generatePlaintext();
//Encrypt the data to the public key and then attempt to decrypt with the private key
const encryptedValue = Api256.encrypt(plaintext, keys.publicKey, signingKeys.privateKey);
const decryptedValue = Api256.decrypt(encryptedValue, keys.privateKey);
decryptedValue === plaintext; //true
```
### 单跳转换加密示例
```
import * as Recrypt from "@ironcorelabs/recrypt-wasm-binding";
//Create a new Recrypt API instance
const Api256 = new Recrypt.Api256();
//Generate both a user key pair and a signing key pair
const userKeys = Api256.generateKeyPair();
const signingKeys = Api256.generateEd25519KeyPair();
//Generate a plaintext to encrypt
const plaintext = Api256.generatePlaintext();
//Encrypt the data to the user public key
const encryptedValue = Api256.encrypt(plaintext, userKeys.publicKey, signingKeys.privateKey);
//Generate a second public/private key pair as the target of the transform. This will allow the encrypted data to be
//transformed to this second key pair and allow it to be decrypted.
const deviceKeys = Api256.generateKeyPair();
//Generate a transform key from the user private key to the device public key
const userToDeviceTransformKey = Api256.generateTransformKey(userKeys.privateKey, deviceKeys.publicKey, signingKeys.privateKey);
//Transform the encrypted data (without decrypting it!) so that it can be decrypted with the second key pair
const transformedEncryptedValue = Api256.transform(encryptedValue, userToDeviceTransformKey, signingKeys.privateKey);
//Decrypt the data using the second private key
const decryptedValue = Api256.decrypt(transformedEncryptedValue, deviceKeys.privateKey);
decryptedValue === plaintext; //true
```
## 类型
此库包含一个 [TypeScript 定义](index.d.ts) 文件,其中显示了可用的类和方法。
## 本地环境准备
在运行此库的基准测试和单元测试之前,需要先安装一些本地依赖项。
- [安装 Rust](https://www.rust-lang.org/en-US/install.html)。必须安装 Rust 才能编译 Rust 绑定到 WASM
- 正确的 Rust 目标:运行 `rustup target add wasm32-unknown-unknown` 以添加编译 WASM 所需的 `wasm32-unknown-unknown` 目标。
- 通过 `cargo install wasm-bindgen-cli` 安装 `wasm-bindgen`。
- 从此仓库根目录运行 `yarn` 以安装所有 JS 依赖项。
## 编译 WebAssembly 模块
运行 `yarn compile` 来编译 Rust 代码并生成 WASM 模块。生成的 `.wasm` 文件和 wasm-bindgen 存根将位于 `target` 目录中。默认情况下我们在发布模式下编译。编译是运行以下单元测试或基准测试的前提条件。
## 基准测试
确保先运行 `yarn compile`。
为了在浏览器中运行基准测试,可以运行 `yarn benchmark`。这将在 [http://localhost:8080](http://localhost:8080) 启动一个 webpack 服务,打开后会自动开始运行单元测试并在屏幕上显示结果和开发者控制台。
## 单元测试
确保先运行 `yarn compile`。
单元测试可以通过两种方式运行:
- `yarn test` 将通过 Chrome Headless 在命令行中运行测试,并在最后报告结果。
- `yarn testInBrowser` 将在 [http://localhost:8080](http://localhost:8080) 启动一个 webpack 服务,您可以直接访问该地址在浏览器中查看测试结果。
## 许可证
Recrypt-wasm-binding 在 [GNU Affero General Public License](LICENSE) 下许可。我们也提供商业许可证 - [email](mailto:info@ironcorelabs.com) 了解更多信息。
版权所有 (c) 2021 IronCore Labs, Inc. 保留所有权利。
标签:AI工具, JavaScript 绑定, NPM 包, Recrypt, Rust, WebAssembly, 前端安全, 前端性能, 加密库, 可视化界面, 安全通信, 异步加载, 数据加解密, 数据可视化, 模块捆绑, 浏览器加密, 端到端加密, 网络流量审计, 自动化攻击