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
//! Fluent builder for PBKDF2-HMAC-SHA512 key derivation.
//!
//! See [`Pbkdf2Builder`].
use crate;
use crateDEFAULT_PBKDF2_ITERATIONS;
use cratederive_pbkdf2_key;
use crateAescryptError;
/// Fluent builder around [`crate::derive_pbkdf2_key`] with secure defaults.
///
/// `Pbkdf2Builder::new()` starts from a fresh CSPRNG-generated 16-byte salt
/// (via [`secure-gate`]'s `Salt16::from_random`) and
/// [`DEFAULT_PBKDF2_ITERATIONS`] iterations. Use [`with_salt`](Self::with_salt)
/// or [`with_iterations`](Self::with_iterations) to override either, then
/// [`derive_secure`](Self::derive_secure) to write the key into a caller
/// buffer or [`derive_secure_new`](Self::derive_secure_new) to allocate one.
///
/// # Errors
///
/// All [`Pbkdf2Builder::derive_secure*`](Self::derive_secure) methods can
/// return [`AescryptError::Crypto`] if the underlying PBKDF2 implementation
/// rejects its parameters.
///
/// # Security
///
/// - Defaults to 300 000 PBKDF2-HMAC-SHA512 iterations and a CSPRNG-backed
/// salt; these are safe for new files. Lower the iteration count only if
/// you have measured your platform.
/// - Salt and derived key live in [`secure-gate`] aliases that zeroize on
/// drop. Passwords pass through scoped `with_secret` reveals only.
/// - This builder is `Send + Sync`. It holds a salt secret but no shared
/// mutable state, so multiple threads can construct and consume their own
/// builders concurrently.
///
/// # Examples
///
/// ```
/// use aescrypt_rs::{Pbkdf2Builder, PasswordString, aliases::Aes256Key32};
///
/// let password = PasswordString::new("my-secret-password".to_string());
///
/// // Use defaults (300k iterations, random salt from `Pbkdf2Builder::new()`).
/// let mut key = Aes256Key32::new([0u8; 32]);
/// Pbkdf2Builder::new()
/// .with_salt([0x42; 16]) // Fixed salt for reproducible doctest
/// .derive_secure(&password, &mut key)?;
///
/// // Or get a new key directly.
/// let _derived_key = Pbkdf2Builder::new()
/// .with_salt([0x42; 16])
/// .derive_secure_new(&password)?;
/// # Ok::<(), aescrypt_rs::AescryptError>(())
/// ```
///
/// # See also
///
/// - [`crate::derive_pbkdf2_key`] — the underlying primitive.
/// - [`crate::encrypt()`] — full v3 encryption pipeline that uses PBKDF2
/// internally.
///
/// [`secure-gate`]: https://github.com/Slurp9187/secure-gate