@h-ai/crypto 0.1.0-alpha.65 → 0.1.0-alpha.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -4,9 +4,11 @@
4
4
 
5
5
  [Framework home](../../README.en.md) · Related modules: [@h-ai/cache](../cache/README.en.md) · [@h-ai/core](../core/README.en.md) · [@h-ai/reldb](../reldb/README.en.md)
6
6
 
7
+ ## Introduction
8
+
7
9
  A cryptography module for asymmetric encryption, hashing, symmetric encryption, and password hashing.
8
10
 
9
- ## Capabilities
11
+ ## Core features
10
12
 
11
13
  - Asymmetric cryptography (key generation, encryption/decryption, signing/verification)
12
14
  - Hashing (hash, HMAC, verification)
@@ -15,22 +17,26 @@ A cryptography module for asymmetric encryption, hashing, symmetric encryption,
15
17
  - Transport encryption (`crypto.transport`: server manager + client `encryptedFetch`)
16
18
  - Shared server/browser support (Node.js / browser)
17
19
 
18
- ## Security notice
20
+ ## Getting started
21
+
22
+ ### Security notice
19
23
 
20
24
  - **SM4 encryption returns a structured payload by default**: `crypto.symmetric.encrypt(data, key)` uses CBC + a random IV by default and returns `{ mode, ciphertext, iv, encoding }`. Pass that object to `crypto.symmetric.decrypt(payload, key)` to decrypt.
21
25
  - **SM4 `deriveKey(password, salt)` is not a KDF**: it performs a single SM3 hash and offers no resistance to password cracking. **Never use it for password storage**. Use `crypto.password.hash(password)` for password hashing and PBKDF2 / scrypt / Argon2 for password-based key derivation.
22
26
  - **IVs must be unique**: never reuse an IV with the same key. Prefer default CBC or `encryptWithIV()` to generate one for each call. Transmit and store the IV with the ciphertext; the IV can be public.
23
27
 
24
- ## Quick start
28
+ ### Quick start
25
29
 
26
30
  ```ts
27
31
  import { crypto } from '@h-ai/crypto'
28
32
 
29
33
  // 初始化(使用前必须调用)
30
- await crypto.init()
34
+ const initialized = await crypto.init()
35
+ if (!initialized.success)
36
+ throw new Error(initialized.error.message)
31
37
  ```
32
38
 
33
- ### Asymmetric cryptography (encryption / signing)
39
+ #### Asymmetric cryptography (encryption / signing)
34
40
 
35
41
  ```ts
36
42
  // 生成密钥对
@@ -61,7 +67,7 @@ crypto.asymmetric.isValidPublicKey(publicKey) // true
61
67
  crypto.asymmetric.isValidPrivateKey(privateKey) // true
62
68
  ```
63
69
 
64
- ### Hashing (Hash / HMAC / verification)
70
+ #### Hashing (Hash / HMAC / verification)
65
71
 
66
72
  ```ts
67
73
  // 字符串哈希
@@ -85,7 +91,7 @@ if (hash.success) {
85
91
  }
86
92
  ```
87
93
 
88
- ### Symmetric encryption (default CBC / structured payload)
94
+ #### Symmetric encryption (default CBC / structured payload)
89
95
 
90
96
  ```ts
91
97
  // 生成随机密钥和 IV
@@ -130,7 +136,7 @@ crypto.symmetric.isValidKey(key) // true
130
136
  crypto.symmetric.isValidIV(iv) // true
131
137
  ```
132
138
 
133
- ### Password hashing (salted iterations)
139
+ #### Password hashing (salted iterations)
134
140
 
135
141
  ```ts
136
142
  // 哈希密码(输出格式: $hai$<iterations>$<salt>$<hash>)
@@ -152,7 +158,7 @@ if (hashed.success) {
152
158
  }
153
159
  ```
154
160
 
155
- ### Transport encryption (crypto.transport)
161
+ #### Transport encryption (crypto.transport)
156
162
 
157
163
  The `crypto.transport` namespace provides transport encryption, allowing `@h-ai/serv`, `@h-ai/kit`, and `@h-ai/api-client` to share protocol constants and payload formats.
158
164
 
@@ -163,7 +169,7 @@ For typical applications, use the higher-level integrations:
163
169
 
164
170
  Call `crypto.transport.createServer()` / `createClient()` directly only for custom runtimes, tests, or manually integrated HTTP key exchange endpoints.
165
171
 
166
- #### Workflow
172
+ ##### Workflow
167
173
 
168
174
  1. Call `await crypto.init()` to initialize `crypto.asymmetric` and `crypto.symmetric`.
169
175
  2. On the server, create a `manager` with `crypto.transport.createServer()`. It holds the server key pair and stores client public keys. Inject a shared `keyStore` for multi-node deployments.
@@ -217,14 +223,14 @@ if (!server.success)
217
223
 
218
224
  Access protocol constants through `crypto.transport.protocol` (or `TRANSPORT_PROTOCOL`): `X-Client-Id`, `X-Encrypted`, and the default `/_hai/key-exchange`.
219
225
 
220
- ### Close the module
226
+ #### Close the module
221
227
 
222
228
  ```ts
223
229
  // 使用完毕后关闭,释放内部状态
224
230
  await crypto.close()
225
231
  ```
226
232
 
227
- ## Error handling
233
+ ### Error handling
228
234
 
229
235
  All operations return `HaiResult<T>`:
230
236
 
@@ -235,7 +241,11 @@ if (!result.success) {
235
241
  }
236
242
  ```
237
243
 
238
- ## Tests
244
+ ### Optional integrations
245
+
246
+ The admin console's online encryption demo calls the Crypto SM3/SM4 APIs directly. SM4 uses a random temporary key and IV and verifies decryption before showing success; keys are not saved. Changing input clears previous results. Random digests or Base64 must not be presented as cryptographic results.
247
+
248
+ ## Development and testing
239
249
 
240
250
  ```bash
241
251
  pnpm --filter @h-ai/crypto test
@@ -244,5 +254,3 @@ pnpm --filter @h-ai/crypto test
244
254
  ## License
245
255
 
246
256
  Apache-2.0
247
-
248
- The admin console's online encryption demo calls the Crypto SM3/SM4 APIs directly. SM4 uses a random temporary key and IV and verifies decryption before showing success; keys are not saved. Changing input clears previous results. Random digests or Base64 must not be presented as cryptographic results.
package/README.ja.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  [フレームワークのホーム](../../README.ja.md) · 関連モジュール:[@h-ai/cache](../cache/README.ja.md) · [@h-ai/core](../core/README.ja.md) · [@h-ai/reldb](../reldb/README.ja.md)
6
6
 
7
+ ## 概要
8
+
7
9
  非対称暗号、ハッシュ、対称暗号、パスワードハッシュを提供する暗号モジュールです。
8
10
 
9
11
  ## 主な機能
@@ -15,22 +17,26 @@
15
17
  - 通信暗号化(`crypto.transport`:サーバー管理機能 + クライアントの `encryptedFetch`)
16
18
  - サーバー・ブラウザー共通(Node.js / ブラウザー)
17
19
 
18
- ## セキュリティに関する注意
20
+ ## 使い始める
21
+
22
+ ### セキュリティに関する注意
19
23
 
20
24
  - **SM4 の既定の暗号化結果は構造化された暗号文**:`crypto.symmetric.encrypt(data, key)` は既定で CBC + ランダム IV を使用し、`{ mode, ciphertext, iv, encoding }` を返します。復号時はこのオブジェクトを `crypto.symmetric.decrypt(payload, key)` に渡します。
21
25
  - **SM4 の `deriveKey(password, salt)` は KDF ではありません**:SM3 を 1 回実行するだけで、パスワードの総当たり攻撃への耐性はありません。**パスワード保存には使用禁止**です。パスワードハッシュには `crypto.password.hash(password)`、パスワードからの鍵導出には PBKDF2 / scrypt / Argon2 を使用してください。
22
26
  - **IV は毎回一意にしてください**:同じ鍵で IV を再利用してはいけません。既定の CBC または `encryptWithIV()` による毎回の自動生成を推奨します。IV は暗号文と一緒に送信・保存します(公開可能で、秘密にする必要はありません)。
23
27
 
24
- ## クイックスタート
28
+ ### クイックスタート
25
29
 
26
30
  ```ts
27
31
  import { crypto } from '@h-ai/crypto'
28
32
 
29
33
  // 初始化(使用前必须调用)
30
- await crypto.init()
34
+ const initialized = await crypto.init()
35
+ if (!initialized.success)
36
+ throw new Error(initialized.error.message)
31
37
  ```
32
38
 
33
- ### 非対称暗号(暗号化・復号 / 署名・検証)
39
+ #### 非対称暗号(暗号化・復号 / 署名・検証)
34
40
 
35
41
  ```ts
36
42
  // 生成密钥对
@@ -61,7 +67,7 @@ crypto.asymmetric.isValidPublicKey(publicKey) // true
61
67
  crypto.asymmetric.isValidPrivateKey(privateKey) // true
62
68
  ```
63
69
 
64
- ### ハッシュ(Hash / HMAC / 検証)
70
+ #### ハッシュ(Hash / HMAC / 検証)
65
71
 
66
72
  ```ts
67
73
  // 字符串哈希
@@ -85,7 +91,7 @@ if (hash.success) {
85
91
  }
86
92
  ```
87
93
 
88
- ### 対称暗号(既定の CBC / 構造化暗号文)
94
+ #### 対称暗号(既定の CBC / 構造化暗号文)
89
95
 
90
96
  ```ts
91
97
  // 生成随机密钥和 IV
@@ -130,7 +136,7 @@ crypto.symmetric.isValidKey(key) // true
130
136
  crypto.symmetric.isValidIV(iv) // true
131
137
  ```
132
138
 
133
- ### パスワードハッシュ(ソルト付き反復)
139
+ #### パスワードハッシュ(ソルト付き反復)
134
140
 
135
141
  ```ts
136
142
  // 哈希密码(输出格式: $hai$<iterations>$<salt>$<hash>)
@@ -152,7 +158,7 @@ if (hashed.success) {
152
158
  }
153
159
  ```
154
160
 
155
- ### 通信暗号化(crypto.transport)
161
+ #### 通信暗号化(crypto.transport)
156
162
 
157
163
  通信暗号化は `crypto.transport` 名前空間で提供し、`@h-ai/serv`、`@h-ai/kit`、`@h-ai/api-client` で同じプロトコル定数とペイロード形式を再利用します。
158
164
 
@@ -163,7 +169,7 @@ if (hashed.success) {
163
169
 
164
170
  `crypto.transport.createServer()` / `createClient()` の直接呼び出しは、独自ランタイム、テスト、HTTP 鍵交換 endpoint を自分で接続する場合に限って推奨します。
165
171
 
166
- #### 使用手順
172
+ ##### 使用手順
167
173
 
168
174
  1. `await crypto.init()` を呼び、`crypto.asymmetric` と `crypto.symmetric` を初期化します。
169
175
  2. サーバーで `crypto.transport.createServer()` を呼び、`manager` を作成します。サーバー鍵ペアとクライアント公開鍵を管理します。複数ノードでは共有 `keyStore` を注入できます。
@@ -217,14 +223,14 @@ if (!server.success)
217
223
 
218
224
  プロトコル定数は `crypto.transport.protocol`(または `TRANSPORT_PROTOCOL`)で取得できます:`X-Client-Id`、`X-Encrypted`、既定の `/_hai/key-exchange`。
219
225
 
220
- ### モジュールの終了
226
+ #### モジュールの終了
221
227
 
222
228
  ```ts
223
229
  // 使用完毕后关闭,释放内部状态
224
230
  await crypto.close()
225
231
  ```
226
232
 
227
- ## エラー処理
233
+ ### エラー処理
228
234
 
229
235
  すべての操作は `HaiResult<T>` を返します:
230
236
 
@@ -235,14 +241,16 @@ if (!result.success) {
235
241
  }
236
242
  ```
237
243
 
238
- ## テスト
244
+ ### 任意の連携
245
+
246
+ 管理画面のオンライン暗号化デモは Crypto の SM3/SM4 API を直接呼びます。SM4 はランダムな一時鍵と IV を使い、成功表示前に復号を検証します。鍵は保存しません。入力が変わると古い結果を消去し、ランダムなダイジェストや Base64 を暗号処理の結果として表示してはいけません。
247
+
248
+ ## 開発とテスト
239
249
 
240
250
  ```bash
241
251
  pnpm --filter @h-ai/crypto test
242
252
  ```
243
253
 
244
- ## License
254
+ ## ライセンス
245
255
 
246
256
  Apache-2.0
247
-
248
- 管理画面のオンライン暗号化デモは Crypto の SM3/SM4 API を直接呼びます。SM4 はランダムな一時鍵と IV を使い、成功表示前に復号を検証します。鍵は保存しません。入力が変わると古い結果を消去し、ランダムなダイジェストや Base64 を暗号処理の結果として表示してはいけません。
package/README.md CHANGED
@@ -4,33 +4,39 @@
4
4
 
5
5
  [框架首页](../../README.md) · 相关模块:[@h-ai/cache](../cache/README.md) · [@h-ai/core](../core/README.md) · [@h-ai/reldb](../reldb/README.md)
6
6
 
7
+ ## 简介
8
+
7
9
  加密模块,提供非对称加密、哈希、对称加密与密码哈希能力。
8
10
 
9
- ## 支持的能力
11
+ ## 核心功能清单
10
12
 
11
13
  - 非对称加密(密钥生成、加解密、签名验签)
12
14
  - 哈希(哈希、HMAC、验证)
13
15
  - 对称加密(ECB/CBC 模式)
14
16
  - 密码哈希(加盐迭代哈希)
15
17
  - 传输加密(`crypto.transport`:服务端管理器 + 客户端 `encryptedFetch`)
16
- - 前后端通用(Node.js / 浏览器)
18
+ - SM2/SM3/SM4 与传输支持 Node.js / 浏览器;密码哈希仅服务端
19
+
20
+ ## 使用入门
17
21
 
18
- ## 安全声明
22
+ ### 安全声明
19
23
 
20
24
  - **SM4 默认加密返回结构化密文**:`crypto.symmetric.encrypt(data, key)` 默认使用 CBC + 随机 IV,返回 `{ mode, ciphertext, iv, encoding }`;解密时把该对象传给 `crypto.symmetric.decrypt(payload, key)`。
21
25
  - **SM4 `deriveKey(password, salt)` 不是 KDF**:仅为单次 SM3 哈希,不具备密码爆破抗性,**禁止用于密码存储**。如需密码哈希,使用 `crypto.password.hash(password)`;如需从密码派生密钥,请采用 PBKDF2 / scrypt / Argon2。
22
26
  - **IV 必须唯一**:相同密钥下禁止复用 IV;推荐使用默认 CBC 或 `encryptWithIV()` 每次自动生成。密文与 IV 需一同传输与存储(IV 可公开,不必保密)。
23
27
 
24
- ## 快速开始
28
+ ### 快速开始
25
29
 
26
30
  ```ts
27
31
  import { crypto } from '@h-ai/crypto'
28
32
 
29
33
  // 初始化(使用前必须调用)
30
- await crypto.init()
34
+ const initialized = await crypto.init()
35
+ if (!initialized.success)
36
+ throw new Error(initialized.error.message)
31
37
  ```
32
38
 
33
- ### 非对称加密(加解密 / 签名验签)
39
+ #### 非对称加密(加解密 / 签名验签)
34
40
 
35
41
  ```ts
36
42
  // 生成密钥对
@@ -61,7 +67,7 @@ crypto.asymmetric.isValidPublicKey(publicKey) // true
61
67
  crypto.asymmetric.isValidPrivateKey(privateKey) // true
62
68
  ```
63
69
 
64
- ### 哈希(Hash / HMAC / 验证)
70
+ #### 哈希(Hash / HMAC / 验证)
65
71
 
66
72
  ```ts
67
73
  // 字符串哈希
@@ -85,7 +91,7 @@ if (hash.success) {
85
91
  }
86
92
  ```
87
93
 
88
- ### 对称加密(CBC 默认 / 结构化密文)
94
+ #### 对称加密(CBC 默认 / 结构化密文)
89
95
 
90
96
  ```ts
91
97
  // 生成随机密钥和 IV
@@ -130,7 +136,7 @@ crypto.symmetric.isValidKey(key) // true
130
136
  crypto.symmetric.isValidIV(iv) // true
131
137
  ```
132
138
 
133
- ### 密码哈希(加盐迭代)
139
+ #### 密码哈希(加盐迭代)
134
140
 
135
141
  ```ts
136
142
  // 哈希密码(输出格式: $hai$<iterations>$<salt>$<hash>)
@@ -152,7 +158,7 @@ if (hashed.success) {
152
158
  }
153
159
  ```
154
160
 
155
- ### 传输加密(crypto.transport)
161
+ #### 传输加密(crypto.transport)
156
162
 
157
163
  传输加密统一通过 `crypto.transport` 命名空间提供,供 `@h-ai/serv`、`@h-ai/kit` 与 `@h-ai/api-client` 复用同一套协议常量和载荷格式。
158
164
 
@@ -163,7 +169,7 @@ if (hashed.success) {
163
169
 
164
170
  只有在自定义运行时、测试或你确实要自己接 HTTP 协商端点时,才建议直接调用 `crypto.transport.createServer()` / `createClient()`。
165
171
 
166
- #### 使用流程
172
+ ##### 使用流程
167
173
 
168
174
  1. 先 `await crypto.init()`,确保 `crypto.asymmetric` 与 `crypto.symmetric` 已初始化。
169
175
  2. 服务端调用 `crypto.transport.createServer()` 创建 `manager`;它持有服务端密钥对,并负责保存客户端公钥。多节点部署时可注入共享 `keyStore`。
@@ -217,14 +223,14 @@ if (!server.success)
217
223
 
218
224
  协议常量通过 `crypto.transport.protocol`(或 `TRANSPORT_PROTOCOL`)访问:`X-Client-Id`、`X-Encrypted`、默认 `/_hai/key-exchange`。
219
225
 
220
- ### 关闭模块
226
+ #### 关闭模块
221
227
 
222
228
  ```ts
223
229
  // 使用完毕后关闭,释放内部状态
224
230
  await crypto.close()
225
231
  ```
226
232
 
227
- ## 错误处理
233
+ ### 错误处理
228
234
 
229
235
  所有操作返回 `HaiResult<T>`:
230
236
 
@@ -235,14 +241,16 @@ if (!result.success) {
235
241
  }
236
242
  ```
237
243
 
238
- ## 测试
244
+ ### 可选集成
245
+
246
+ 管理端在线加密演示直接调用 Crypto SM3/SM4 API,SM4 使用随机临时密钥和 IV,并在显示成功前解密校验;密钥不保存。输入改变即清空旧结果,禁止用随机摘要或 Base64 伪装算法结果。
247
+
248
+ ## 开发测试
239
249
 
240
250
  ```bash
241
251
  pnpm --filter @h-ai/crypto test
242
252
  ```
243
253
 
244
- ## License
254
+ ## 许可证
245
255
 
246
256
  Apache-2.0
247
-
248
- 管理端在线加密演示直接调用 Crypto SM3/SM4 API,SM4 使用随机临时密钥和 IV,并在显示成功前解密校验;密钥不保存。输入改变即清空旧结果,禁止用随机摘要或 Base64 伪装算法结果。
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@h-ai/crypto",
3
3
  "type": "module",
4
- "version": "0.1.0-alpha.65",
4
+ "version": "0.1.0-alpha.67",
5
5
  "description": "Hai Framework cryptography module (asymmetric, symmetric, and hash).",
6
6
  "license": "Apache-2.0",
7
7
  "exports": {
@@ -23,12 +23,12 @@
23
23
  "node": "^22.22.2 || ^24.15.0 || >=26.0.0"
24
24
  },
25
25
  "dependencies": {
26
- "@h-ai/core": "0.1.0-alpha.65",
26
+ "@h-ai/core": "0.1.0-alpha.67",
27
27
  "sm-crypto": "^0.5.7"
28
28
  },
29
29
  "optionalDependencies": {
30
- "@h-ai/cache": "0.1.0-alpha.65",
31
- "@h-ai/reldb": "0.1.0-alpha.65"
30
+ "@h-ai/cache": "0.1.0-alpha.67",
31
+ "@h-ai/reldb": "0.1.0-alpha.67"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^26.6.5",