From 9e29d2d572e74c890174442c614a70c53aca1bcc Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 20 Dec 2016 16:17:00 +0100 Subject: [PATCH] Added Crypto::pbkdf2 --- crypto.hpp | 12 ++++++++++++ tests/crypto_test.cpp | 11 +++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/crypto.hpp b/crypto.hpp index 366b6cd..f99e31d 100644 --- a/crypto.hpp +++ b/crypto.hpp @@ -133,6 +133,18 @@ namespace SimpleWeb { return hash; } + + /// Returns empty string on error. key_size is in bytes. + static std::string pbkdf2(const std::string &password, const std::string &salt, int iterations = 4096, int key_size = 256 / 8) { + std::string key; + key.resize(key_size); + auto success = PKCS5_PBKDF2_HMAC_SHA1(password.c_str(), password.size(), + reinterpret_cast(salt.c_str()), salt.size(), iterations, + key_size, reinterpret_cast(&key[0])); + if (!success) + return std::string(); + return key; + } }; } #endif /* CRYPTO_HPP */ diff --git a/tests/crypto_test.cpp b/tests/crypto_test.cpp index 9ea9c2e..28684a9 100644 --- a/tests/crypto_test.cpp +++ b/tests/crypto_test.cpp @@ -1,7 +1,4 @@ -#include #include -#include -#include #include #include "crypto.hpp" @@ -40,29 +37,27 @@ const vector > sha512_string_tests = { }; int main() { - //Testing SimpleWeb::Crypt::Base64 for(auto& string_test: base64_string_tests) { assert(Crypto::Base64::encode(string_test.first)==string_test.second); assert(Crypto::Base64::decode(string_test.second)==string_test.first); } - //Testing SimpleWeb::Crypt::MD5 for(auto& string_test: md5_string_tests) assert(Crypto::to_hex_string(Crypto::md5(string_test.first)) == string_test.second); - //Testing SimpleWeb::Crypt::sha1 for(auto& string_test: sha1_string_tests) assert(Crypto::to_hex_string(Crypto::sha1(string_test.first)) == string_test.second); - //Testing SimpleWeb::Crypt::sha256 for(auto& string_test: sha256_string_tests) assert(Crypto::to_hex_string(Crypto::sha256(string_test.first)) == string_test.second); - //Testing SimpleWeb::Crypt::sha512 for(auto& string_test: sha512_string_tests) assert(Crypto::to_hex_string(Crypto::sha512(string_test.first)) == string_test.second); //Testing iterations assert(Crypto::to_hex_string(Crypto::sha1("Test", 1)) == "640ab2bae07bedc4c163f679a746f7ab7fb5d1fa"); assert(Crypto::to_hex_string(Crypto::sha1("Test", 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5"); + + assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 4096, 128 / 8)) == "f66df50f8aaa11e4d9721e1312ff2e66"); + assert(Crypto::to_hex_string(Crypto::pbkdf2("Password", "Salt", 8192, 512 / 8)) == "a941ccbc34d1ee8ebbd1d34824a419c3dc4eac9cbc7c36ae6c7ca8725e2b618a6ad22241e787af937b0960cf85aa8ea3a258f243e05d3cc9b08af5dd93be046c"); }