aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/passhash.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-02-01 16:29:38 +0000
committerlloyd <[email protected]>2010-02-01 16:29:38 +0000
commit454e45b7c4fece11a7f43ffa412148b4a274c90f (patch)
tree5ae87c2104fba534548e59fa477d6a5f2f5a5e29 /doc/examples/passhash.cpp
parentae6a404ec14cc3c86a96cd3e5c67c9c23be38147 (diff)
Modify the S2K interface. Instead of being stateful in terms of the salt
and iteration count, force it to be passed to each call to derive_key. So remove current_salt, set_iterations, new_random_salt, and change_salt functions from S2K interface. Update examples and test application to match. While I was in there, change the passhash example to use 64 bit salts and 128 bit PBKDF2 outputs.
Diffstat (limited to 'doc/examples/passhash.cpp')
-rw-r--r--doc/examples/passhash.cpp27
1 files changed, 19 insertions, 8 deletions
diff --git a/doc/examples/passhash.cpp b/doc/examples/passhash.cpp
index 24f7ff674..8c50de072 100644
--- a/doc/examples/passhash.cpp
+++ b/doc/examples/passhash.cpp
@@ -55,18 +55,26 @@ int main(int argc, char* argv[])
return 0;
}
+const u32bit SALT_BYTES = 8; // 64 bits of salt
+const u32bit PBKDF_OUTPUT_LEN = 16; // 128 bits output
+const u32bit KDF_ITERATIONS = 100000;
+
std::string password_hash(const std::string& pass,
RandomNumberGenerator& rng)
{
PKCS5_PBKDF2 kdf(new HMAC(new SHA_160));
- kdf.set_iterations(10000);
- kdf.new_random_salt(rng, 6); // 48 bits
+ SecureVector<byte> salt(SALT_BYTES);
+ rng.randomize(&salt[0], salt.size());
+
+ // Encode the salt plus 96 bits of PBKDF2 output
Pipe pipe(new Base64_Encoder);
pipe.start_msg();
- pipe.write(kdf.current_salt());
- pipe.write(kdf.derive_key(12, pass).bits_of());
+ pipe.write(salt);
+ pipe.write(kdf.derive_key(PBKDF_OUTPUT_LEN, pass,
+ &salt[0], salt.size(),
+ KDF_ITERATIONS).bits_of());
pipe.end_msg();
return pipe.read_all_as_string();
@@ -81,12 +89,15 @@ bool password_hash_ok(const std::string& pass, const std::string& hash)
SecureVector<byte> hash_bin = pipe.read_all();
- PKCS5_PBKDF2 kdf(new HMAC(new SHA_160));
+ if(hash_bin.size() != (PBKDF_OUTPUT_LEN + SALT_BYTES))
+ return false;
- kdf.set_iterations(10000);
- kdf.change_salt(hash_bin, 6);
+ PKCS5_PBKDF2 kdf(new HMAC(new SHA_160));
- SecureVector<byte> cmp = kdf.derive_key(12, pass).bits_of();
+ SecureVector<byte> cmp = kdf.derive_key(
+ PBKDF_OUTPUT_LEN, pass,
+ &hash_bin[0], SALT_BYTES,
+ KDF_ITERATIONS).bits_of();
return same_mem(cmp.begin(), hash_bin.begin() + 6, 12);
}