1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! Password-based Key Derivation Functions (KDF) for low-entropy inputs.
//!
//! 用于低熵输入的基于密码的密钥派生函数 (KDF)。
//!
//! ## Overview | 概述
//!
//! Password-based KDFs are specifically designed to work with low-entropy inputs
//! such as user passwords, PINs, or passphrases. They use computational cost
//! (time, memory, or both) to make brute-force attacks impractical, even when
//! the input has limited entropy.
//!
//! 基于密码的 KDF 专门设计用于处理低熵输入,
//! 如用户密码、PIN 或密码短语。它们使用计算成本(时间、内存或两者)
//! 使暴力攻击变得不切实际,即使输入的熵有限。
//!
//! ## Supported Algorithms | 支持的算法
//!
//! ### PBKDF2 (Password-Based Key Derivation Function 2)
//! - **Standard**: RFC 2898, PKCS #5
//! - **Type**: Time-hard function
//! - **Resistance**: CPU-based attacks
//! - **Tunable Parameter**: Iteration count
//!
//! ### Argon2 (Winner of Password Hashing Competition)
//! - **Standard**: RFC 9106
//! - **Type**: Memory-hard function
//! - **Resistance**: GPU, FPGA, and ASIC attacks
//! - **Tunable Parameters**: Memory cost, time cost, parallelism
//!
//! ## Security Comparison | 安全性对比
//!
//! | Algorithm | Type | Memory Usage | GPU Resistance | ASIC Resistance |
//! |-----------|------|--------------|----------------|-----------------|
//! | PBKDF2 | Time-hard | Low | Poor | Poor |
//! | Argon2 | Memory-hard | High | Excellent | Excellent |
//!
//! ## Attack Resistance | 攻击抗性
//!
//! ### PBKDF2 Limitations | PBKDF2 局限性
//! - Vulnerable to GPU-based attacks
//! - Vulnerable to FPGA/ASIC attacks
//! - Only time-based cost function
//!
//! - 易受基于 GPU 的攻击
//! - 易受 FPGA/ASIC 攻击
//! - 仅基于时间的成本函数
//!
//! ### Argon2 Advantages | Argon2 优势
//! - Memory-hard: requires significant RAM
//! - Resistant to specialized hardware
//! - Configurable time/memory trade-offs
//!
//! - 内存困难:需要大量 RAM
//! - 抗专用硬件
//! - 可配置的时间/内存权衡
//!
//! ## Usage Guidelines | 使用指南
//!
//! - **New Applications**: Use Argon2 for better security
//! - **Legacy Compatibility**: Use PBKDF2 when required
//! - **High Security**: Use Argon2 with high memory cost
//! - **Resource Constrained**: Use PBKDF2 with high iteration count
//!
//! - **新应用**: 使用 Argon2 获得更好的安全性
//! - **遗留兼容性**: 需要时使用 PBKDF2
//! - **高安全性**: 使用高内存成本的 Argon2
//! - **资源受限**: 使用高迭代次数的 PBKDF2
use crateHashAlgorithm;
use ;
/// Argon2 algorithm parameters for customizing security vs performance trade-offs.
///
/// 用于自定义安全性与性能权衡的 Argon2 算法参数。
///
/// ## Parameters | 参数
///
/// These parameters control the computational cost of Argon2:
///
/// 这些参数控制 Argon2 的计算成本:
///
/// - **Memory Cost (m_cost)**: Amount of memory used in KB
/// - **Time Cost (t_cost)**: Number of iterations
/// - **Parallelism (p_cost)**: Number of parallel threads
///
/// - **内存成本 (m_cost)**: 使用的内存量(KB)
/// - **时间成本 (t_cost)**: 迭代次数
/// - **并行度 (p_cost)**: 并行线程数
///
/// ## Recommended Values | 推荐值
///
/// | Use Case | m_cost | t_cost | p_cost | Security Level |
/// |----------|--------|--------|--------|----------------|
/// | Interactive | 65536 | 2 | 1 | Medium |
/// | Server | 262144 | 3 | 4 | High |
/// | Sensitive | 1048576 | 4 | 8 | Very High |
///
/// ## Security vs Performance | 安全性与性能
///
/// Higher values provide better security but require more resources:
/// - Increase m_cost to resist memory-optimized attacks
/// - Increase t_cost to resist time-optimized attacks
/// - Increase p_cost to utilize multiple CPU cores
///
/// 更高的值提供更好的安全性但需要更多资源:
/// - 增加 m_cost 以抵抗内存优化攻击
/// - 增加 t_cost 以抵抗时间优化攻击
/// - 增加 p_cost 以利用多个 CPU 核心
/// Password-based Key Derivation Function algorithm enumeration.
///
/// 基于密码的密钥派生函数算法枚举。
///
/// ## Algorithm Selection | 算法选择
///
/// Choose based on your security and compatibility requirements:
///
/// 根据您的安全性和兼容性要求选择:
///
/// - **Argon2**: Recommended for new applications (better security)
/// - **PBKDF2**: Use for legacy compatibility or resource constraints
///
/// - **Argon2**: 推荐用于新应用(更好的安全性)
/// - **PBKDF2**: 用于遗留兼容性或资源约束
/// Builder for constructing password-based KDF algorithm instances.
///
/// 用于构建基于密码的 KDF 算法实例的构建器。
///
/// ## Usage Pattern | 使用模式
///
/// The builder provides both default and custom parameter methods:
/// - Default methods use secure, recommended parameters
/// - Custom parameter methods allow fine-tuning for specific requirements
///
/// 构建器提供默认和自定义参数方法:
/// - 默认方法使用安全、推荐的参数
/// - 自定义参数方法允许针对特定要求进行微调
///
/// ## Security Guidelines | 安全指南
///
/// - **For new applications**: Use Argon2 for better security
/// - **For compatibility**: Use PBKDF2 when required by standards
/// - **Parameter tuning**: Test performance vs security trade-offs
/// - **Regular updates**: Review and update parameters as hardware improves
///
/// - **新应用**: 使用 Argon2 获得更好的安全性
/// - **兼容性**: 标准要求时使用 PBKDF2
/// - **参数调优**: 测试性能与安全性的权衡
/// - **定期更新**: 随着硬件改进审查和更新参数
;
use crateKdfPasswordWrapper;