@maiyunnet/kebab 9.27.1 → 9.28.0

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.
@@ -0,0 +1,3 @@
1
+ import type { IPasswordOptions } from '#kebab/lib/crypto.js';
2
+ export declare function hash(password: string, options: IPasswordOptions): Promise<string | false>;
3
+ export declare function verify(password: string, encoded: string): Promise<boolean | null>;
@@ -0,0 +1,60 @@
1
+ import * as crypto from 'node:crypto';
2
+ import { promisify } from 'node:util';
3
+ const argon2 = typeof crypto.argon2 === 'function' ? promisify(crypto.argon2) : undefined;
4
+ function valid(memory, passes, parallelism) {
5
+ return Number.isInteger(memory) && Number.isInteger(passes) && Number.isInteger(parallelism) &&
6
+ parallelism >= 1 && parallelism <= 8 && memory >= 8 * parallelism && memory <= 262144 &&
7
+ passes >= 1 && passes <= 10;
8
+ }
9
+ function base64(value) {
10
+ return value.toString('base64').replace(/=+$/, '');
11
+ }
12
+ export async function hash(password, options) {
13
+ try {
14
+ const { memory = 65536, passes = 3, parallelism = 1 } = options;
15
+ if (!argon2 || !valid(memory, passes, parallelism)) {
16
+ return false;
17
+ }
18
+ const salt = crypto.randomBytes(16);
19
+ const tag = await argon2('argon2id', {
20
+ message: password, nonce: salt, tagLength: 32, memory, passes, parallelism,
21
+ });
22
+ return `$argon2id$v=19$m=${memory},t=${passes},p=${parallelism}$${base64(salt)}$${base64(tag)}`;
23
+ }
24
+ catch {
25
+ return false;
26
+ }
27
+ }
28
+ export async function verify(password, encoded) {
29
+ if (typeof encoded !== 'string' || encoded.length > 256) {
30
+ return false;
31
+ }
32
+ const match = /^\$argon2id\$v=19\$m=([1-9]\d*),t=([1-9]\d*),p=([1-9]\d*)\$([A-Za-z0-9+/]+)\$([A-Za-z0-9+/]+)$/.exec(encoded);
33
+ if (match?.[0] !== encoded) {
34
+ return false;
35
+ }
36
+ const memory = Number(match[1]);
37
+ const passes = Number(match[2]);
38
+ const parallelism = Number(match[3]);
39
+ if (!valid(memory, passes, parallelism)) {
40
+ return false;
41
+ }
42
+ const salt = Buffer.from(match[4], 'base64');
43
+ const expected = Buffer.from(match[5], 'base64');
44
+ if (salt.length < 8 || salt.length > 64 || expected.length < 16 || expected.length > 64 ||
45
+ base64(salt) !== match[4] || base64(expected) !== match[5]) {
46
+ return false;
47
+ }
48
+ if (!argon2) {
49
+ return null;
50
+ }
51
+ try {
52
+ const actual = await argon2('argon2id', {
53
+ message: password, nonce: salt, tagLength: expected.length, memory, passes, parallelism,
54
+ });
55
+ return crypto.timingSafeEqual(actual, expected);
56
+ }
57
+ catch {
58
+ return null;
59
+ }
60
+ }
package/doc/kebab-rag.md CHANGED
@@ -233,6 +233,33 @@ export default class Order extends sMod {
233
233
 
234
234
  `_$partition` 用于数据库原生分区,与 `index` 选项按表名后缀访问分表的机制不同。事务提交、回滚及连接生命周期保持原约定。
235
235
 
236
+ ## 密码散列
237
+
238
+ `lib/crypto` 提供异步 Argon2id 密码散列和验证,需要 Node.js 24.7.0+ 的原生 `crypto.argon2` 支持。
239
+
240
+ ```ts
241
+ import * as lCrypto from '@maiyunnet/kebab/lib/crypto.js';
242
+
243
+ const encoded = await lCrypto.passwordHash(password);
244
+ if (encoded !== false) {
245
+ const matched = await lCrypto.passwordVerify(password, encoded);
246
+ // matched 为 true:匹配;false:不匹配或格式无效;null:计算失败。
247
+ }
248
+
249
+ // 可选:根据服务器资源调整计算成本,未传入的字段仍使用默认值。
250
+ const custom = await lCrypto.passwordHash(password, {
251
+ 'memory': 65536,
252
+ 'passes': 3,
253
+ 'parallelism': 1,
254
+ });
255
+ ```
256
+
257
+ 默认使用 64 MiB 内存、3 轮、并行度 1、16 字节随机盐和 32 字节散列。`memory` 单位为 KiB;只开放内存、轮数和并行度,取值范围见 `IPasswordOptions`。这些参数控制每次计算成本,内存和轮数越高,正常登录也越耗资源;调整前应在目标机器测试并发负载。默认值高于 [OWASP 的最低配置](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html),通常可直接使用。自定义低成本参数不保证相同的抗破解强度。
258
+
259
+ 数据库保存完整的 `$argon2id$v=19$...` 字符串即可,不需要另存盐或参数。验证从该字符串读取参数,默认值调整不会影响旧密码;需要提升旧密码成本时,在验证成功后重新调用 `passwordHash()` 并保存结果。只从可信存储读取待验证散列,不让客户端提供散列或计算参数。
260
+
261
+ `passwordHash()` 成功返回 PHC 字符串;选项无效、环境不支持、随机数生成或计算失败返回 `false`。`passwordVerify()` 匹配返回 `true`,密码不匹配、格式无效、算法不支持或参数越界返回 `false`,环境不支持或计算失败返回 `null`。调用方用 `=== false`、`=== null` 区分结果,不能把验证时的系统故障当作密码错误。方法不截断、去空格或归一化密码;密码策略、登录限速和数据库更新由应用负责。
262
+
236
263
  query-pagination.md
237
264
  ---
238
265
 
@@ -1850,7 +1877,7 @@ index/variables/VER.md
1850
1877
 
1851
1878
  # Variable: VER
1852
1879
 
1853
- > `const` **VER**: `"9.27.1"` = `'9.27.1'`
1880
+ > `const` **VER**: `"9.28.0"` = `'9.28.0'`
1854
1881
 
1855
1882
  Defined in: [index.ts:9](https://github.com/maiyunnet/kebab/blob/master/index.ts#L9)
1856
1883
 
@@ -116558,7 +116585,7 @@ hash 或 hmac 加密文件
116558
116585
 
116559
116586
  #### encoding?
116560
116587
 
116561
- `"base64"` \| `"hex"` \| `"base64url"`
116588
+ `"base64"` \| `"base64url"` \| `"hex"`
116562
116589
 
116563
116590
  ### Returns
116564
116591
 
@@ -116683,6 +116710,82 @@ hash 或 hmac 加密
116683
116710
 
116684
116711
  `Buffer`
116685
116712
 
116713
+ lib/crypto/functions/passwordHash.md
116714
+ ---
116715
+
116716
+ [**Documents for @maiyunnet/kebab**](../../../index.md)
116717
+
116718
+ ***
116719
+
116720
+ [Documents for @maiyunnet/kebab](../../../index.md) / [lib/crypto](../index.md) / passwordHash
116721
+
116722
+ # Function: passwordHash()
116723
+
116724
+ > **passwordHash**(`password`, `options?`): `Promise`\<`string` \| `false`\>
116725
+
116726
+ Defined in: [lib/crypto.ts:487](https://github.com/maiyunnet/kebab/blob/master/lib/crypto.ts#L487)
116727
+
116728
+ 使用 Argon2id 生成包含随机盐和计算参数的密码散列
116729
+ 需要 Node.js 24.7.0+;固定使用 16 字节随机盐和 32 字节散列,不修改或截断密码。
116730
+
116731
+ ## Parameters
116732
+
116733
+ ### password
116734
+
116735
+ `string`
116736
+
116737
+ 原始密码,按 UTF-8 编码;长度和强度由应用校验
116738
+
116739
+ ### options?
116740
+
116741
+ [`IPasswordOptions`](../interfaces/IPasswordOptions.md) = `{}`
116742
+
116743
+ 计算成本,省略时使用 64 MiB 内存、3 轮和并行度 1
116744
+
116745
+ ## Returns
116746
+
116747
+ `Promise`\<`string` \| `false`\>
116748
+
116749
+ 可直接保存的 PHC 字符串;参数无效、运行环境不支持或计算失败时返回 false
116750
+
116751
+ lib/crypto/functions/passwordVerify.md
116752
+ ---
116753
+
116754
+ [**Documents for @maiyunnet/kebab**](../../../index.md)
116755
+
116756
+ ***
116757
+
116758
+ [Documents for @maiyunnet/kebab](../../../index.md) / [lib/crypto](../index.md) / passwordVerify
116759
+
116760
+ # Function: passwordVerify()
116761
+
116762
+ > **passwordVerify**(`password`, `encoded`): `Promise`\<`boolean` \| `null`\>
116763
+
116764
+ Defined in: [lib/crypto.ts:498](https://github.com/maiyunnet/kebab/blob/master/lib/crypto.ts#L498)
116765
+
116766
+ 使用散列中保存的参数验证密码
116767
+ 接受 Argon2id v19,成本范围与 IPasswordOptions 相同,盐为 8~64 字节、散列为 16~64 字节。
116768
+
116769
+ ## Parameters
116770
+
116771
+ ### password
116772
+
116773
+ `string`
116774
+
116775
+ 原始密码,按 UTF-8 编码
116776
+
116777
+ ### encoded
116778
+
116779
+ `string`
116780
+
116781
+ 数据库存储的 PHC 字符串,不接受客户端指定的散列
116782
+
116783
+ ## Returns
116784
+
116785
+ `Promise`\<`boolean` \| `null`\>
116786
+
116787
+ 匹配返回 true;不匹配、格式不支持或参数越界返回 false;环境不支持或计算失败时返回 null
116788
+
116686
116789
  lib/crypto/functions/privateDecrypt.md
116687
116790
  ---
116688
116791
 
@@ -117187,6 +117290,10 @@ lib/crypto/index.md
117187
117290
 
117188
117291
  # lib/crypto
117189
117292
 
117293
+ ## Interfaces
117294
+
117295
+ - [IPasswordOptions](interfaces/IPasswordOptions.md)
117296
+
117190
117297
  ## Variables
117191
117298
 
117192
117299
  - [AES\_256\_CBC](variables/AES_256_CBC.md)
@@ -117210,6 +117317,8 @@ lib/crypto/index.md
117210
117317
  - [generateKeyPair](functions/generateKeyPair.md)
117211
117318
  - [hashHmac](functions/hashHmac.md)
117212
117319
  - [hashHmacFile](functions/hashHmacFile.md)
117320
+ - [passwordHash](functions/passwordHash.md)
117321
+ - [passwordVerify](functions/passwordVerify.md)
117213
117322
  - [privateDecrypt](functions/privateDecrypt.md)
117214
117323
  - [privateEncrypt](functions/privateEncrypt.md)
117215
117324
  - [publicDecrypt](functions/publicDecrypt.md)
@@ -117220,6 +117329,51 @@ lib/crypto/index.md
117220
117329
  - [uuid](functions/uuid.md)
117221
117330
  - [verify](functions/verify.md)
117222
117331
 
117332
+ lib/crypto/interfaces/IPasswordOptions.md
117333
+ ---
117334
+
117335
+ [**Documents for @maiyunnet/kebab**](../../../index.md)
117336
+
117337
+ ***
117338
+
117339
+ [Documents for @maiyunnet/kebab](../../../index.md) / [lib/crypto](../index.md) / IPasswordOptions
117340
+
117341
+ # Interface: IPasswordOptions
117342
+
117343
+ Defined in: [lib/crypto.ts:471](https://github.com/maiyunnet/kebab/blob/master/lib/crypto.ts#L471)
117344
+
117345
+ 密码散列的计算成本,未指定的选项使用默认值
117346
+
117347
+ ## Properties
117348
+
117349
+ ### memory?
117350
+
117351
+ > `optional` **memory?**: `number`
117352
+
117353
+ Defined in: [lib/crypto.ts:473](https://github.com/maiyunnet/kebab/blob/master/lib/crypto.ts#L473)
117354
+
117355
+ 内存成本,单位 KiB,默认 65536(64 MiB);整数,至少为并行度的 8 倍,最多 262144
117356
+
117357
+ ***
117358
+
117359
+ ### parallelism?
117360
+
117361
+ > `optional` **parallelism?**: `number`
117362
+
117363
+ Defined in: [lib/crypto.ts:477](https://github.com/maiyunnet/kebab/blob/master/lib/crypto.ts#L477)
117364
+
117365
+ 并行度,默认 1;整数,范围 1~8
117366
+
117367
+ ***
117368
+
117369
+ ### passes?
117370
+
117371
+ > `optional` **passes?**: `number`
117372
+
117373
+ Defined in: [lib/crypto.ts:475](https://github.com/maiyunnet/kebab/blob/master/lib/crypto.ts#L475)
117374
+
117375
+ 计算轮数,默认 3;整数,范围 1~10
117376
+
117223
117377
  lib/crypto/variables/AES_256_CBC.md
117224
117378
  ---
117225
117379
 
@@ -136259,7 +136413,7 @@ Defined in: [sys/mod.ts:90](https://github.com/maiyunnet/kebab/blob/master/sys/m
136259
136413
  Defined in: [sys/mod.ts:87](https://github.com/maiyunnet/kebab/blob/master/sys/mod.ts#L87)
136260
136414
 
136261
136415
  表的分区字段名,如 ['time_push']
136262
- save、remove、refresh 默认会把这些字段加入 WHERE 条件,与主键一起查找记录。
136416
+ 只填分区字段,不用额外填写主键;save、remove、refresh 默认会把主键一起加入 WHERE。
136263
136417
  读取记录后不要修改主键或分区字段;留空时只使用主键。
136264
136418
 
136265
136419
  ***
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export declare const VER = "9.27.1";
7
+ export declare const VER = "9.28.0";
8
8
  /** --- 框架根目录,以 / 结尾 --- */
9
9
  export declare const ROOT_PATH: string;
10
10
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export const VER = '9.27.1';
8
+ export const VER = '9.28.0';
9
9
  // --- 服务端用的路径 ---
10
10
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
11
11
  /** --- /xxx/xxx --- */
package/lib/crypto.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * Project: Kebab, User: JianSuoQiYue
3
3
  * Date: 2019-4-2 14:01:06
4
- * Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47, 2026-07-29 11:51
5
4
  */
6
5
  import * as crypto from 'crypto';
7
6
  /**
@@ -182,3 +181,28 @@ export declare function base64Decode(data: string, encoding?: 'utf8'): string;
182
181
  * @param options 选项
183
182
  */
184
183
  export declare function uuid(options?: crypto.RandomUUIDOptions): string;
184
+ /** --- 密码散列的计算成本,未指定的选项使用默认值 --- */
185
+ export interface IPasswordOptions {
186
+ /** --- 内存成本,单位 KiB,默认 65536(64 MiB);整数,至少为并行度的 8 倍,最多 262144 --- */
187
+ 'memory'?: number;
188
+ /** --- 计算轮数,默认 3;整数,范围 1~10 --- */
189
+ 'passes'?: number;
190
+ /** --- 并行度,默认 1;整数,范围 1~8 --- */
191
+ 'parallelism'?: number;
192
+ }
193
+ /**
194
+ * --- 使用 Argon2id 生成包含随机盐和计算参数的密码散列 ---
195
+ * 需要 Node.js 24.7.0+;固定使用 16 字节随机盐和 32 字节散列,不修改或截断密码。
196
+ * @param password 原始密码,按 UTF-8 编码;长度和强度由应用校验
197
+ * @param options 计算成本,省略时使用 64 MiB 内存、3 轮和并行度 1
198
+ * @returns 可直接保存的 PHC 字符串;参数无效、运行环境不支持或计算失败时返回 false
199
+ */
200
+ export declare function passwordHash(password: string, options?: IPasswordOptions): Promise<string | false>;
201
+ /**
202
+ * --- 使用散列中保存的参数验证密码 ---
203
+ * 接受 Argon2id v19,成本范围与 IPasswordOptions 相同,盐为 8~64 字节、散列为 16~64 字节。
204
+ * @param password 原始密码,按 UTF-8 编码
205
+ * @param encoded 数据库存储的 PHC 字符串,不接受客户端指定的散列
206
+ * @returns 匹配返回 true;不匹配、格式不支持或参数越界返回 false;环境不支持或计算失败时返回 null
207
+ */
208
+ export declare function passwordVerify(password: string, encoded: string): Promise<boolean | null>;
package/lib/crypto.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * Project: Kebab, User: JianSuoQiYue
3
3
  * Date: 2019-4-2 14:01:06
4
- * Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47, 2026-07-29 11:51
5
4
  */
6
5
  import * as crypto from 'crypto';
7
6
  // --- 库和定义 ---
8
7
  import * as lFs from '#kebab/lib/fs.js';
9
8
  import * as lCore from '#kebab/lib/core.js';
9
+ import * as aPassword from '#kebab/abox/crypto-password.js';
10
10
  // --- 非对称加密 ---
11
11
  /**
12
12
  * --- 创建非对称秘钥 ---
@@ -320,3 +320,23 @@ export function base64Decode(data, encoding = 'utf8') {
320
320
  export function uuid(options) {
321
321
  return crypto.randomUUID(options);
322
322
  }
323
+ /**
324
+ * --- 使用 Argon2id 生成包含随机盐和计算参数的密码散列 ---
325
+ * 需要 Node.js 24.7.0+;固定使用 16 字节随机盐和 32 字节散列,不修改或截断密码。
326
+ * @param password 原始密码,按 UTF-8 编码;长度和强度由应用校验
327
+ * @param options 计算成本,省略时使用 64 MiB 内存、3 轮和并行度 1
328
+ * @returns 可直接保存的 PHC 字符串;参数无效、运行环境不支持或计算失败时返回 false
329
+ */
330
+ export function passwordHash(password, options = {}) {
331
+ return aPassword.hash(password, options);
332
+ }
333
+ /**
334
+ * --- 使用散列中保存的参数验证密码 ---
335
+ * 接受 Argon2id v19,成本范围与 IPasswordOptions 相同,盐为 8~64 字节、散列为 16~64 字节。
336
+ * @param password 原始密码,按 UTF-8 编码
337
+ * @param encoded 数据库存储的 PHC 字符串,不接受客户端指定的散列
338
+ * @returns 匹配返回 true;不匹配、格式不支持或参数越界返回 false;环境不支持或计算失败时返回 null
339
+ */
340
+ export function passwordVerify(password, encoded) {
341
+ return aPassword.verify(password, encoded);
342
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.27.1",
3
+ "version": "9.28.0",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/mod.d.ts CHANGED
@@ -45,7 +45,7 @@ export default class Mod {
45
45
  protected static _$primary: string;
46
46
  /**
47
47
  * --- 表的分区字段名,如 ['time_push'] ---
48
- * save、remove、refresh 默认会把这些字段加入 WHERE 条件,与主键一起查找记录。
48
+ * 只填分区字段,不用额外填写主键;save、remove、refresh 默认会把主键一起加入 WHERE。
49
49
  * 读取记录后不要修改主键或分区字段;留空时只使用主键。
50
50
  */
51
51
  protected static _$partition: string[];
package/sys/mod.js CHANGED
@@ -53,7 +53,7 @@ export default class Mod {
53
53
  static _$primary = 'id';
54
54
  /**
55
55
  * --- 表的分区字段名,如 ['time_push'] ---
56
- * save、remove、refresh 默认会把这些字段加入 WHERE 条件,与主键一起查找记录。
56
+ * 只填分区字段,不用额外填写主键;save、remove、refresh 默认会把主键一起加入 WHERE。
57
57
  * 读取记录后不要修改主键或分区字段;留空时只使用主键。
58
58
  */
59
59
  static _$partition = [];