@h-ai/crypto 0.1.0-alpha5
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/LICENSE +202 -0
- package/README.md +162 -0
- package/dist/index.d.ts +332 -0
- package/dist/index.js +820 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [2026] [idealworld.group]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# @h-ai/crypto
|
|
2
|
+
|
|
3
|
+
加密模块,提供非对称加密、哈希、对称加密与密码哈希能力。
|
|
4
|
+
|
|
5
|
+
## 支持的能力
|
|
6
|
+
|
|
7
|
+
- 非对称加密(密钥生成、加解密、签名验签)
|
|
8
|
+
- 哈希(哈希、HMAC、验证)
|
|
9
|
+
- 对称加密(ECB/CBC 模式)
|
|
10
|
+
- 密码哈希(加盐迭代哈希)
|
|
11
|
+
- 前后端通用(Node.js / 浏览器)
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { crypto } from '@h-ai/crypto'
|
|
17
|
+
|
|
18
|
+
// 初始化(使用前必须调用)
|
|
19
|
+
await crypto.init()
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 非对称加密(加解密 / 签名验签)
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// 生成密钥对
|
|
26
|
+
const keyPair = crypto.asymmetric.generateKeyPair()
|
|
27
|
+
if (!keyPair.success)
|
|
28
|
+
throw new Error(keyPair.error.message)
|
|
29
|
+
const { publicKey, privateKey } = keyPair.data
|
|
30
|
+
|
|
31
|
+
// 加密 / 解密
|
|
32
|
+
const encrypted = crypto.asymmetric.encrypt('Hello', publicKey)
|
|
33
|
+
if (encrypted.success) {
|
|
34
|
+
const decrypted = crypto.asymmetric.decrypt(encrypted.data, privateKey)
|
|
35
|
+
// decrypted.data === 'Hello'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 输出 base64 格式密文
|
|
39
|
+
const b64 = crypto.asymmetric.encrypt('Hello', publicKey, { outputFormat: 'base64' })
|
|
40
|
+
|
|
41
|
+
// 签名 / 验签
|
|
42
|
+
const sig = crypto.asymmetric.sign('important data', privateKey)
|
|
43
|
+
if (sig.success) {
|
|
44
|
+
const valid = crypto.asymmetric.verify('important data', sig.data, publicKey)
|
|
45
|
+
// valid.data === true
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 校验密钥格式
|
|
49
|
+
crypto.asymmetric.isValidPublicKey(publicKey) // true
|
|
50
|
+
crypto.asymmetric.isValidPrivateKey(privateKey) // true
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 哈希(Hash / HMAC / 验证)
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
// 字符串哈希
|
|
57
|
+
const hash = crypto.hash.hash('Hello!')
|
|
58
|
+
// hash.data → 64 字符十六进制哈希值
|
|
59
|
+
|
|
60
|
+
// Uint8Array 输入
|
|
61
|
+
const buf = new TextEncoder().encode('Hello!')
|
|
62
|
+
const hashBuf = crypto.hash.hash(buf)
|
|
63
|
+
|
|
64
|
+
// 十六进制编码输入
|
|
65
|
+
const hashHex = crypto.hash.hash('48656c6c6f21', { inputEncoding: 'hex' })
|
|
66
|
+
|
|
67
|
+
// HMAC
|
|
68
|
+
const hmac = crypto.hash.hmac('message', 'secret-key')
|
|
69
|
+
|
|
70
|
+
// 验证哈希是否匹配
|
|
71
|
+
if (hash.success) {
|
|
72
|
+
const matched = crypto.hash.verify('Hello!', hash.data)
|
|
73
|
+
// matched.data === true
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 对称加密(ECB / CBC)
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// 生成随机密钥和 IV
|
|
81
|
+
const key = crypto.symmetric.generateKey()
|
|
82
|
+
const iv = crypto.symmetric.generateIV()
|
|
83
|
+
|
|
84
|
+
// ECB 模式(默认)
|
|
85
|
+
const ecbEnc = crypto.symmetric.encrypt('data', key)
|
|
86
|
+
if (ecbEnc.success) {
|
|
87
|
+
const ecbDec = crypto.symmetric.decrypt(ecbEnc.data, key)
|
|
88
|
+
// ecbDec.data === 'data'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// CBC 模式(需指定 IV)
|
|
92
|
+
const cbcEnc = crypto.symmetric.encrypt('data', key, { mode: 'cbc', iv })
|
|
93
|
+
if (cbcEnc.success) {
|
|
94
|
+
const cbcDec = crypto.symmetric.decrypt(cbcEnc.data, key, { mode: 'cbc', iv })
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 快捷方式:自动生成 IV 的 CBC 加解密
|
|
98
|
+
const withIV = crypto.symmetric.encryptWithIV('data', key)
|
|
99
|
+
if (withIV.success) {
|
|
100
|
+
const dec = crypto.symmetric.decryptWithIV(withIV.data.ciphertext, key, withIV.data.iv)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 输出 base64 格式
|
|
104
|
+
const b64Enc = crypto.symmetric.encrypt('data', key, { outputFormat: 'base64' })
|
|
105
|
+
|
|
106
|
+
// 从密码派生密钥
|
|
107
|
+
const derivedKey = crypto.symmetric.deriveKey('my-password', 'random-salt')
|
|
108
|
+
|
|
109
|
+
// 校验密钥/IV 格式
|
|
110
|
+
crypto.symmetric.isValidKey(key) // true
|
|
111
|
+
crypto.symmetric.isValidIV(iv) // true
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 密码哈希(加盐迭代)
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
// 哈希密码(输出格式: $hai$<iterations>$<salt>$<hash>)
|
|
118
|
+
const hashed = crypto.password.hash('myPassword123')
|
|
119
|
+
|
|
120
|
+
// 自定义盐值长度和迭代次数
|
|
121
|
+
const custom = crypto.password.hash('myPassword123', {
|
|
122
|
+
saltLength: 32,
|
|
123
|
+
iterations: 20000,
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// 验证密码
|
|
127
|
+
if (hashed.success) {
|
|
128
|
+
const ok = crypto.password.verify('myPassword123', hashed.data)
|
|
129
|
+
// ok.data === true
|
|
130
|
+
|
|
131
|
+
const wrong = crypto.password.verify('wrongPassword', hashed.data)
|
|
132
|
+
// wrong.data === false
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 关闭模块
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
// 使用完毕后关闭,释放内部状态
|
|
140
|
+
await crypto.close()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## 错误处理
|
|
144
|
+
|
|
145
|
+
所有操作返回 `HaiResult<T>`:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const result = crypto.asymmetric.encrypt('data', publicKey)
|
|
149
|
+
if (!result.success) {
|
|
150
|
+
// result.error.code / result.error.message
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## 测试
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
pnpm --filter @h-ai/crypto test
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
Apache-2.0
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import * as _h_ai_core from '@h-ai/core';
|
|
2
|
+
import { HaiResult } from '@h-ai/core';
|
|
3
|
+
|
|
4
|
+
declare const HaiCryptoError: {
|
|
5
|
+
readonly INVALID_INPUT: _h_ai_core.HaiErrorDef;
|
|
6
|
+
readonly INVALID_KEY: _h_ai_core.HaiErrorDef;
|
|
7
|
+
readonly NOT_INITIALIZED: _h_ai_core.HaiErrorDef;
|
|
8
|
+
readonly INIT_FAILED: _h_ai_core.HaiErrorDef;
|
|
9
|
+
readonly KEY_GENERATION_FAILED: _h_ai_core.HaiErrorDef;
|
|
10
|
+
readonly ENCRYPTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
11
|
+
readonly DECRYPTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
12
|
+
readonly SIGN_FAILED: _h_ai_core.HaiErrorDef;
|
|
13
|
+
readonly VERIFY_FAILED: _h_ai_core.HaiErrorDef;
|
|
14
|
+
readonly HASH_FAILED: _h_ai_core.HaiErrorDef;
|
|
15
|
+
readonly HMAC_FAILED: _h_ai_core.HaiErrorDef;
|
|
16
|
+
readonly INVALID_IV: _h_ai_core.HaiErrorDef;
|
|
17
|
+
};
|
|
18
|
+
/** 密文模式:0=C1C2C3(旧版),1=C1C3C2(国标) */
|
|
19
|
+
type CipherMode = 0 | 1;
|
|
20
|
+
/** 非对称密钥对 */
|
|
21
|
+
interface KeyPair {
|
|
22
|
+
/** 公钥(十六进制字符串,包含 04 前缀为非压缩格式) */
|
|
23
|
+
publicKey: string;
|
|
24
|
+
/** 私钥(十六进制字符串,64 字符) */
|
|
25
|
+
privateKey: string;
|
|
26
|
+
}
|
|
27
|
+
/** 非对称加密选项 */
|
|
28
|
+
interface AsymmetricEncryptOptions {
|
|
29
|
+
/** 密文模式:0=C1C2C3(旧版),1=C1C3C2(国标,默认) */
|
|
30
|
+
cipherMode?: CipherMode;
|
|
31
|
+
/** 输出格式 */
|
|
32
|
+
outputFormat?: 'hex' | 'base64';
|
|
33
|
+
}
|
|
34
|
+
/** 签名选项 */
|
|
35
|
+
interface SignOptions {
|
|
36
|
+
/** 是否对数据进行哈希(默认 true) */
|
|
37
|
+
hash?: boolean;
|
|
38
|
+
/** 用户 ID(默认 "1234567812345678") */
|
|
39
|
+
userId?: string;
|
|
40
|
+
}
|
|
41
|
+
/** 哈希选项 */
|
|
42
|
+
interface HashOptions {
|
|
43
|
+
/** 输入编码 */
|
|
44
|
+
inputEncoding?: 'utf8' | 'hex';
|
|
45
|
+
}
|
|
46
|
+
/** 对称加密模式 */
|
|
47
|
+
type SymmetricMode = 'ecb' | 'cbc';
|
|
48
|
+
/** 对称加密选项 */
|
|
49
|
+
interface SymmetricOptions {
|
|
50
|
+
/** 加密模式 */
|
|
51
|
+
mode?: SymmetricMode;
|
|
52
|
+
/** IV 向量(CBC 模式必需,32 个十六进制字符) */
|
|
53
|
+
iv?: string;
|
|
54
|
+
/** 输入编码 */
|
|
55
|
+
inputEncoding?: 'utf8' | 'hex';
|
|
56
|
+
/** 输出格式 */
|
|
57
|
+
outputFormat?: 'hex' | 'base64';
|
|
58
|
+
}
|
|
59
|
+
/** 带 IV 加密结果 */
|
|
60
|
+
interface EncryptWithIVResult {
|
|
61
|
+
/** 密文 */
|
|
62
|
+
ciphertext: string;
|
|
63
|
+
/** IV 向量 */
|
|
64
|
+
iv: string;
|
|
65
|
+
}
|
|
66
|
+
/** 密码哈希配置 */
|
|
67
|
+
interface PasswordConfig {
|
|
68
|
+
/** 盐值长度(默认 16) */
|
|
69
|
+
saltLength?: number;
|
|
70
|
+
/** 迭代次数(默认 10000) */
|
|
71
|
+
iterations?: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 非对称加密操作接口
|
|
75
|
+
*
|
|
76
|
+
* 通过 `crypto.asymmetric` 访问,需先调用 `crypto.init()`。
|
|
77
|
+
*/
|
|
78
|
+
interface AsymmetricOperations {
|
|
79
|
+
/**
|
|
80
|
+
* 生成密钥对
|
|
81
|
+
*
|
|
82
|
+
* @returns 成功时包含公私钥对;失败时返回 KEY_GENERATION_FAILED
|
|
83
|
+
*/
|
|
84
|
+
generateKeyPair: () => HaiResult<KeyPair>;
|
|
85
|
+
/**
|
|
86
|
+
* 非对称加密
|
|
87
|
+
*
|
|
88
|
+
* @param data - 待加密明文
|
|
89
|
+
* @param publicKey - 公钥(十六进制,支持带/不带 04 前缀)
|
|
90
|
+
* @param options - 加密选项(密文模式、输出格式)
|
|
91
|
+
* @returns 成功时返回密文字符串;失败时返回 INVALID_KEY 或 ENCRYPTION_FAILED
|
|
92
|
+
*/
|
|
93
|
+
encrypt: (data: string, publicKey: string, options?: AsymmetricEncryptOptions) => HaiResult<string>;
|
|
94
|
+
/**
|
|
95
|
+
* 非对称解密
|
|
96
|
+
*
|
|
97
|
+
* 自动检测 base64 格式输入并转换为 hex。
|
|
98
|
+
*
|
|
99
|
+
* @param ciphertext - 密文(hex 或 base64)
|
|
100
|
+
* @param privateKey - 私钥(64 字符十六进制)
|
|
101
|
+
* @param options - 解密选项(密文模式需与加密时一致)
|
|
102
|
+
* @returns 成功时返回明文;失败时返回 INVALID_KEY 或 DECRYPTION_FAILED
|
|
103
|
+
*/
|
|
104
|
+
decrypt: (ciphertext: string, privateKey: string, options?: AsymmetricEncryptOptions) => HaiResult<string>;
|
|
105
|
+
/**
|
|
106
|
+
* 签名
|
|
107
|
+
*
|
|
108
|
+
* @param data - 待签名数据
|
|
109
|
+
* @param privateKey - 私钥(64 字符十六进制)
|
|
110
|
+
* @param options - 签名选项(hash 开关、userId)
|
|
111
|
+
* @returns 成功时返回签名字符串;失败时返回 INVALID_KEY 或 SIGN_FAILED
|
|
112
|
+
*/
|
|
113
|
+
sign: (data: string, privateKey: string, options?: SignOptions) => HaiResult<string>;
|
|
114
|
+
/**
|
|
115
|
+
* 验签
|
|
116
|
+
*
|
|
117
|
+
* @param data - 原始数据
|
|
118
|
+
* @param signature - 签名(需与签名时使用相同的 hash/userId 选项)
|
|
119
|
+
* @param publicKey - 公钥(支持带/不带 04 前缀)
|
|
120
|
+
* @param options - 验签选项
|
|
121
|
+
* @returns 成功时返回 boolean;失败时返回 INVALID_KEY 或 VERIFY_FAILED
|
|
122
|
+
*/
|
|
123
|
+
verify: (data: string, signature: string, publicKey: string, options?: SignOptions) => HaiResult<boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* 校验公钥格式是否合法
|
|
126
|
+
*
|
|
127
|
+
* 合法格式:128 字符十六进制(无前缀)或 130 字符(含 04 前缀)。
|
|
128
|
+
*/
|
|
129
|
+
isValidPublicKey: (key: string) => boolean;
|
|
130
|
+
/**
|
|
131
|
+
* 校验私钥格式是否合法
|
|
132
|
+
*
|
|
133
|
+
* 合法格式:64 字符十六进制。
|
|
134
|
+
*/
|
|
135
|
+
isValidPrivateKey: (key: string) => boolean;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 哈希操作接口
|
|
139
|
+
*
|
|
140
|
+
* 通过 `crypto.hash` 访问,需先调用 `crypto.init()`。
|
|
141
|
+
*/
|
|
142
|
+
interface HashOperations {
|
|
143
|
+
/**
|
|
144
|
+
* 计算哈希
|
|
145
|
+
*
|
|
146
|
+
* @param data - 待哈希数据(字符串或 Uint8Array)
|
|
147
|
+
* @param options - 输入编码选项
|
|
148
|
+
* @returns 成功时返回 64 字符十六进制哈希值;失败时返回 HASH_FAILED
|
|
149
|
+
*/
|
|
150
|
+
hash: (data: string | Uint8Array, options?: HashOptions) => HaiResult<string>;
|
|
151
|
+
/**
|
|
152
|
+
* 计算 HMAC
|
|
153
|
+
*
|
|
154
|
+
* 使用 HMAC 算法(RFC 2104)计算消息认证码。
|
|
155
|
+
* 当密钥长度超过块大小(64 字节)时,会先对密钥进行哈希。
|
|
156
|
+
*
|
|
157
|
+
* @param data - 待计算数据
|
|
158
|
+
* @param key - HMAC 密钥
|
|
159
|
+
* @returns 成功时返回 64 字符十六进制 HMAC 值;失败时返回 HMAC_FAILED
|
|
160
|
+
*/
|
|
161
|
+
hmac: (data: string, key: string) => HaiResult<string>;
|
|
162
|
+
/**
|
|
163
|
+
* 验证数据的哈希是否匹配
|
|
164
|
+
*
|
|
165
|
+
* 比较时忽略大小写。
|
|
166
|
+
*
|
|
167
|
+
* @param data - 原始数据
|
|
168
|
+
* @param expectedHash - 期望的哈希值
|
|
169
|
+
* @returns 成功时返回 boolean;失败时返回 HASH_FAILED
|
|
170
|
+
*/
|
|
171
|
+
verify: (data: string, expectedHash: string) => HaiResult<boolean>;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 对称加密操作接口
|
|
175
|
+
*
|
|
176
|
+
* 通过 `crypto.symmetric` 访问,需先调用 `crypto.init()`。
|
|
177
|
+
* 支持 ECB/CBC 两种模式,CBC 模式需要提供 IV。
|
|
178
|
+
*/
|
|
179
|
+
interface SymmetricOperations {
|
|
180
|
+
/** 生成随机密钥(16 字节 = 32 个十六进制字符) */
|
|
181
|
+
generateKey: () => string;
|
|
182
|
+
/** 生成随机 IV(16 字节 = 32 个十六进制字符) */
|
|
183
|
+
generateIV: () => string;
|
|
184
|
+
/**
|
|
185
|
+
* 对称加密
|
|
186
|
+
*
|
|
187
|
+
* @param data - 待加密明文
|
|
188
|
+
* @param key - 密钥(32 字符十六进制)
|
|
189
|
+
* @param options - 加密模式/IV/输出格式
|
|
190
|
+
* @returns 成功时返回密文;失败时返回 INVALID_KEY/INVALID_IV/ENCRYPTION_FAILED
|
|
191
|
+
*/
|
|
192
|
+
encrypt: (data: string, key: string, options?: SymmetricOptions) => HaiResult<string>;
|
|
193
|
+
/**
|
|
194
|
+
* 对称解密
|
|
195
|
+
*
|
|
196
|
+
* 自动检测 base64 格式输入并转换为 hex。
|
|
197
|
+
*
|
|
198
|
+
* @param ciphertext - 密文(hex 或 base64)
|
|
199
|
+
* @param key - 密钥(32 字符十六进制)
|
|
200
|
+
* @param options - 解密模式/IV(需与加密时一致)
|
|
201
|
+
* @returns 成功时返回明文;失败时返回 INVALID_KEY/INVALID_IV/DECRYPTION_FAILED
|
|
202
|
+
*/
|
|
203
|
+
decrypt: (ciphertext: string, key: string, options?: SymmetricOptions) => HaiResult<string>;
|
|
204
|
+
/**
|
|
205
|
+
* 带 IV 加密(CBC 模式,自动生成随机 IV)
|
|
206
|
+
*
|
|
207
|
+
* @param data - 待加密明文
|
|
208
|
+
* @param key - 密钥(32 字符十六进制)
|
|
209
|
+
* @returns 成功时返回 { ciphertext, iv };失败时同 encrypt
|
|
210
|
+
*/
|
|
211
|
+
encryptWithIV: (data: string, key: string) => HaiResult<EncryptWithIVResult>;
|
|
212
|
+
/**
|
|
213
|
+
* 带 IV 解密(CBC 模式)
|
|
214
|
+
*
|
|
215
|
+
* @param ciphertext - 密文
|
|
216
|
+
* @param key - 密钥
|
|
217
|
+
* @param iv - 加密时使用的 IV
|
|
218
|
+
* @returns 成功时返回明文;失败时同 decrypt
|
|
219
|
+
*/
|
|
220
|
+
decryptWithIV: (ciphertext: string, key: string, iv: string) => HaiResult<string>;
|
|
221
|
+
/**
|
|
222
|
+
* 从密码和盐值派生密钥
|
|
223
|
+
*
|
|
224
|
+
* 内部使用哈希(password + salt) 取前 32 字符作为密钥。
|
|
225
|
+
* 注意:此为简单派生,不适用于高安全场景。
|
|
226
|
+
*
|
|
227
|
+
* @param password - 密码
|
|
228
|
+
* @param salt - 盐值
|
|
229
|
+
* @returns 32 字符十六进制密钥
|
|
230
|
+
*/
|
|
231
|
+
deriveKey: (password: string, salt: string) => string;
|
|
232
|
+
/** 校验密钥格式是否合法(32 字符十六进制) */
|
|
233
|
+
isValidKey: (key: string) => boolean;
|
|
234
|
+
/** 校验 IV 格式是否合法(32 字符十六进制) */
|
|
235
|
+
isValidIV: (iv: string) => boolean;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* 密码哈希操作接口
|
|
239
|
+
*
|
|
240
|
+
* 通过 `crypto.password` 访问,需先调用 `crypto.init()`。
|
|
241
|
+
* 使用迭代加盐的方式生成密码哈希。
|
|
242
|
+
*/
|
|
243
|
+
interface PasswordOperations {
|
|
244
|
+
/**
|
|
245
|
+
* 对密码进行哈希
|
|
246
|
+
*
|
|
247
|
+
* 输出格式: `$hai$<iterations>$<salt>$<hash>`
|
|
248
|
+
*
|
|
249
|
+
* @param password - 明文密码(不能为空)
|
|
250
|
+
* @param config - 可选配置(盐值长度、迭代次数)
|
|
251
|
+
* @returns 成功时返回格式化的哈希字符串;失败时返回 INVALID_INPUT 或 HASH_FAILED
|
|
252
|
+
*/
|
|
253
|
+
hash: (password: string, config?: PasswordConfig) => HaiResult<string>;
|
|
254
|
+
/**
|
|
255
|
+
* 验证密码是否匹配
|
|
256
|
+
*
|
|
257
|
+
* 自动从哈希字符串中解析迭代次数和盐值进行重新计算。
|
|
258
|
+
*
|
|
259
|
+
* @param password - 待验证的明文密码
|
|
260
|
+
* @param hash - 存储的哈希值(格式: `$hai$<iterations>$<salt>$<hash>`)
|
|
261
|
+
* @returns 成功时返回 boolean;失败时返回 INVALID_INPUT 或 VERIFY_FAILED
|
|
262
|
+
*/
|
|
263
|
+
verify: (password: string, hash: string) => HaiResult<boolean>;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 加密模块函数接口
|
|
267
|
+
*
|
|
268
|
+
* `crypto` 服务对象的类型定义。使用前必须调用 `init()` 初始化。
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* import { crypto } from '@h-ai/crypto'
|
|
273
|
+
*
|
|
274
|
+
* await crypto.init()
|
|
275
|
+
* const hash = crypto.hash.hash('hello')
|
|
276
|
+
* await crypto.close()
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
interface CryptoFunctions {
|
|
280
|
+
/**
|
|
281
|
+
* 初始化加密模块
|
|
282
|
+
*
|
|
283
|
+
* 创建非对称/哈希/对称/密码哈希操作实例。
|
|
284
|
+
* 重复调用会先关闭再重新初始化。
|
|
285
|
+
*
|
|
286
|
+
* @returns 成功时返回 ok(undefined);失败时返回 INIT_FAILED
|
|
287
|
+
*/
|
|
288
|
+
init: () => Promise<HaiResult<void>>;
|
|
289
|
+
/**
|
|
290
|
+
* 关闭加密模块,释放内部状态
|
|
291
|
+
*
|
|
292
|
+
* 关闭后再访问 asymmetric/hash/symmetric/password 会返回 NOT_INITIALIZED 错误。
|
|
293
|
+
*/
|
|
294
|
+
close: () => Promise<void>;
|
|
295
|
+
/** 是否已初始化 */
|
|
296
|
+
readonly isInitialized: boolean;
|
|
297
|
+
/** 非对称加密操作(未初始化时所有方法返回 NOT_INITIALIZED) */
|
|
298
|
+
readonly asymmetric: AsymmetricOperations;
|
|
299
|
+
/** 哈希操作(未初始化时所有方法返回 NOT_INITIALIZED) */
|
|
300
|
+
readonly hash: HashOperations;
|
|
301
|
+
/** 对称加密操作(未初始化时所有方法返回 NOT_INITIALIZED) */
|
|
302
|
+
readonly symmetric: SymmetricOperations;
|
|
303
|
+
/** 密码哈希操作(未初始化时所有方法返回 NOT_INITIALIZED) */
|
|
304
|
+
readonly password: PasswordOperations;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* @h-ai/crypto — 加密服务主入口
|
|
309
|
+
*
|
|
310
|
+
* 提供统一的 `crypto` 对象,聚合所有结构化加密操作。
|
|
311
|
+
* @module crypto-main
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 加密模块服务对象(统一入口)
|
|
316
|
+
*
|
|
317
|
+
* 使用前必须调用 `crypto.init()` 进行初始化。
|
|
318
|
+
* 未初始化时访问 asymmetric/hash/symmetric/password 的任何方法均返回 NOT_INITIALIZED 错误。
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* import { crypto } from '@h-ai/crypto'
|
|
323
|
+
*
|
|
324
|
+
* await crypto.init()
|
|
325
|
+
* const hash = crypto.hash.hash('hello')
|
|
326
|
+
* const keyPair = crypto.asymmetric.generateKeyPair()
|
|
327
|
+
* await crypto.close()
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
declare const crypto: CryptoFunctions;
|
|
331
|
+
|
|
332
|
+
export { type AsymmetricEncryptOptions, type AsymmetricOperations, type CipherMode, type CryptoFunctions, type EncryptWithIVResult, HaiCryptoError, type HashOperations, type HashOptions, type KeyPair, type PasswordConfig, type PasswordOperations, type SignOptions, type SymmetricMode, type SymmetricOperations, type SymmetricOptions, crypto };
|