@@ -53,14 +53,21 @@ namespace hmac_cpp {
5353
5454 // / \brief Hash choices for PBKDF2
5555 enum class Pbkdf2Hash { Sha1, Sha256, Sha512 };
56-
56+
5757 // / PBKDF2 Security Notes:
5858 // / - Use a random salt of at least 16 bytes and never reuse it.
5959 // / - Choose iterations so the derivation takes about 200–500 ms on 2025 hardware.
6060 // / - Store {salt, iterations} with the ciphertext or hash; these values are public.
6161 // / - Salts and iteration counts must be unique per password.
6262 // / - Example serialization: {magic|ver|prf|salt|iters|dkLen|…}.
6363
64+ // / \brief
65+ struct Pbkdf2Result {
66+ std::vector<uint8_t > salt;
67+ uint32_t iters;
68+ std::vector<uint8_t > key;
69+ };
70+
6471 // / \brief Derives a key from a password using PBKDF2 (RFC 8018)
6572 // / \param password_ptr Pointer to the password buffer
6673 // / \param password_len Length of the password in bytes
@@ -113,6 +120,39 @@ namespace hmac_cpp {
113120 iterations, dk_len, prf);
114121 }
115122
123+ template <typename T>
124+ inline Pbkdf2Result pbkdf2 (
125+ const std::vector<T>& password,
126+ const Pbkdf2Result& params,
127+ Pbkdf2Hash prf = Pbkdf2Hash::Sha256) {
128+ static_assert (std::is_same<T, char >::value || std::is_same<T, uint8_t >::value,
129+ " pbkdf2(vector<T>) supports only char or uint8_t" );
130+ auto key = pbkdf2 (password.data (), password.size (),
131+ params.salt .data (), params.salt .size (),
132+ params.iters , params.key .size (), prf);
133+ return {params.salt , params.iters , std::move (key)};
134+ }
135+
136+ inline Pbkdf2Result pbkdf2 (
137+ const std::string& password,
138+ const Pbkdf2Result& params,
139+ Pbkdf2Hash prf = Pbkdf2Hash::Sha256) {
140+ auto key = pbkdf2 (password.data (), password.size (),
141+ params.salt .data (), params.salt .size (),
142+ params.iters , params.key .size (), prf);
143+ return {params.salt , params.iters , std::move (key)};
144+ }
145+
146+ inline Pbkdf2Result pbkdf2 (
147+ const secure_buffer<uint8_t >& password,
148+ const Pbkdf2Result& params,
149+ Pbkdf2Hash prf = Pbkdf2Hash::Sha256) {
150+ auto key = pbkdf2 (password.data (), password.size (),
151+ params.salt .data (), params.salt .size (),
152+ params.iters , params.key .size (), prf);
153+ return {params.salt , params.iters , std::move (key)};
154+ }
155+
116156 // / \brief Derives PBKDF2 into caller-provided buffer using selected hash.
117157 // / \param prf Hash function to use (SHA1, SHA256, SHA512)
118158 // / \param password_ptr Pointer to the password buffer
0 commit comments