Skip to content

Commit 626aa31

Browse files
add README-CN
1 parent e73fe81 commit 626aa31

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

README-CN.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,147 @@
11
# crypto-js-wasm
22

33
[English](README.md) | [中文](README-CN.md)
4+
5+
---
6+
7+
`crypto-js-wasm` 是基于 JavaScript 和 [WebAssembly](https://webassembly.org/) 的哈西与加密算法库,其灵感来自于[crypto-js](https://github.com/brix/crypto-js)
8+
9+
- **安全**: 得益于 WebAssembly ,crypto-js-wasm的计算过程是**不可见、不可中断**的。
10+
- **高效**: 相比于crypto-js,计算效率最高提升了16倍(见 [Benchmark](https://originjs.org/WASM-benchmark/#/))。
11+
- **浏览器 & Nodejs**: 同时支持 `浏览器``nodejs`.
12+
- **全能**: 支持**15+** 以上的哈西和加密算法,包括常用的 MD5、 SHA-x、 AES、RC4等。
13+
- **ESM**: 基于ES模块语法编写,支持一部运行
14+
15+
16+
17+
## 安装
18+
19+
```bash
20+
npm install originjs@crypto-js-wasm
21+
```
22+
23+
24+
25+
```bash
26+
pnpm install originjs@crypto-js-wasm
27+
```
28+
29+
30+
31+
```bash
32+
yarn add originjs@crypto-js-wasm
33+
```
34+
35+
36+
37+
## 使用
38+
39+
在使用各算法前需调用一次对应的`loadWasm()`,或调用`loadAllWasm()`以加载所有算法的WebAssembly文件。
40+
41+
42+
43+
```javascript
44+
import CryptoJSW from 'crypto-js-wasm';
45+
46+
// (可选) 加载所有 wasm 文件
47+
await CryptoJSW.loadAllWasm();
48+
49+
// 通过 Async/Await 语法调用
50+
await CryptoJSW.MD5.loadWasm();
51+
const rstMD5 = CryptoJSW.MD5('message').toString();
52+
console.log(rstMD5);
53+
54+
// 通过 Promise 语法调用
55+
CryptoJSW.SHA256.loadWasm().then(() => {
56+
const rstSHA256 = CryptoJSW.SHA256('message').toString();
57+
console.log(rstSHA256);
58+
})
59+
```
60+
61+
62+
63+
**目前可用的算法**
64+
65+
- MD5 / HmacMD5
66+
- SHA1 / HmacSHA1
67+
- SHA224 / HmacSHA224
68+
- SHA256 / HmacSHA256
69+
- SHA384 / HmacSHA384
70+
- SHA512 / HmacSHA512
71+
- SHA3 / HmacSHA3
72+
- RIPEMD160 / HmacRIPEMD160
73+
- PBKDF2
74+
- EvpKDF
75+
76+
<br>
77+
78+
- AES
79+
- Blowfish
80+
- DES
81+
- TripleDES
82+
- Rabbit
83+
- RabbitLegacy
84+
- RC4
85+
- RC4Drop
86+
87+
88+
89+
**下一步计划支持**
90+
91+
- RSA
92+
93+
94+
95+
## Benchmark
96+
97+
该 benchmark 结果运行自一台台式机 (i5-4590, 16 GB RAM, Windows 10 Version 21H2 (OSBuild 19044, 1466))。
98+
99+
100+
101+
*Chrome 102.0.5005.63:*
102+
103+
![benchmark_chrome](/home/peterlee/code/crypto-js-wasm-originjs/crypto-js-wasm/benchmark/benchmark_chrome.png)
104+
105+
106+
107+
Firefox 101.0:
108+
109+
![benchmark_firefox](/home/peterlee/code/crypto-js-wasm-originjs/crypto-js-wasm/benchmark/benchmark_firefox.png)
110+
111+
112+
113+
## 开发
114+
115+
```bash
116+
# 安装以来
117+
pnpm install
118+
119+
# 生产构建
120+
pnpm run build
121+
122+
# 运行所有测试
123+
pnpm run test
124+
125+
# 运行所有测试并生成测试覆盖率报告
126+
pnpm run coverage
127+
```
128+
129+
130+
131+
#### 为何我们需要调用异步的 loadWasm?
132+
133+
这是因为 WebAssembly 二进制需要通过 `WebAssembly.instantiate` 加载,并且这是一个异步函数。
134+
135+
`WebAssembly.instantiate `与它的同步实现 `WebAssembly.instance` 相比,前者更受推荐;并且,在许多场景下,`WebAssembly.instance` 无法加载不够小的 WebAssembly 二进制。
136+
137+
138+
139+
#### 为何我们需要以base64编码字符的方式,存储wasm二进制?
140+
141+
因为 `crypto-js-wasm` 需要同时支持 `browser``nodejs` 两种使用场景。相比与 `browser` 中的 `wasm loader` (多数情况下由 webpack, vite 或其他框架提供)以及 `nodejs` 中的 `fs` 方式,这种wasm二进制存储方式是一种相对优雅的方式。
142+
143+
144+
145+
## 版权说明
146+
147+
该项目遵守[木兰宽松许可证](LICENSE)

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- **Safe**: The process of encryption is fully **enclosed** and **invisible** thanks to WebAssembly.
1010
- **Efficient**: Up to 16x **faster** than crypto-js (see [Benchmark](https://originjs.org/WASM-benchmark/#/)).
11+
- **Browser & Nodejs**: Support both `browser` and `nodejs`.
1112
- **Versatile**: **15+** crypto standards supported, including MD5, SHA-x, AES, RC4, etc.
1213
- **ESM**: Crypto standards can be imported as **ES modules**.
1314

@@ -124,3 +125,23 @@ pnpm run test
124125
# run all tests with coverage
125126
pnpm run coverage
126127
```
128+
129+
130+
131+
#### Why do we need a async loadWasm call?
132+
133+
This is because the WebAssembly binary needs to be load by `WebAssembly.instantiate`, and it is async.
134+
135+
The async `WebAssembly.instantiate` is recommended instead of its sync variant `WebAssembly.instance`, and in many cases the `WebAssembly.instance` can not load WebAssembly binary whose size is not small enough.
136+
137+
138+
139+
#### Why do we store wasm binaries in base64-encoded chars?
140+
141+
This is because `crypto-js-wasm` may be used in `browser` or `nodejs`. This is relative elegant implementation comparing with `wasm loader` in `browser`(powered by webpack, vite or something else) or `fs` in `nodejs`.
142+
143+
144+
145+
## License
146+
147+
Distributed under the [Mulan Permissive Software License](LICENSE)

0 commit comments

Comments
 (0)