diff --git a/crypto.hpp b/crypto.hpp index 425c1f8d..4d7a7d70 100644 --- a/crypto.hpp +++ b/crypto.hpp @@ -18,11 +18,11 @@ namespace SimpleWeb { } #endif - //type must support size(), resize() and operator[] namespace Crypto { namespace Base64 { - template - void encode(const type& ascii, type& base64) { + std::string encode(const std::string &ascii) { + std::string base64; + BIO *bio, *b64; BUF_MEM *bptr=BUF_MEM_new(); @@ -48,16 +48,13 @@ namespace SimpleWeb { bptr->data=nullptr; BIO_free_all(b64); - } - template - type encode(const type& ascii) { - type base64; - encode(ascii, base64); + return base64; } - template - void decode(const type& base64, type& ascii) { + std::string decode(const std::string &base64) { + std::string ascii; + //Resize ascii, however, the size is a up to two bytes too large. ascii.resize((6*base64.size())/8); BIO *b64, *bio; @@ -71,77 +68,56 @@ namespace SimpleWeb { ascii.resize(decoded_length); BIO_free_all(b64); - } - template - type decode(const type& base64) { - type ascii; - decode(base64, ascii); + return ascii; } - } - template - void MD5(const type& input, type& hash) { - MD5_CTX context; - MD5_Init(&context); - MD5_Update(&context, &input[0], input.size()); + std::string MD5(const std::string &input, size_t iterations=1) { + std::string hash; + + hash.resize(128 / 8); + ::MD5(reinterpret_cast(&input[0]), input.size(), reinterpret_cast(&hash[0])); + + for (size_t c = 1; c < iterations; ++c) + ::MD5(reinterpret_cast(&hash[0]), hash.size(), reinterpret_cast(&hash[0])); - hash.resize(128/8); - MD5_Final((unsigned char*)&hash[0], &context); - } - template - type MD5(const type& input) { - type hash; - MD5(input, hash); return hash; } - template - void SHA1(const type& input, type& hash) { - SHA_CTX context; - SHA1_Init(&context); - SHA1_Update(&context, &input[0], input.size()); + std::string SHA1(const std::string &input, size_t iterations=1) { + std::string hash; + + hash.resize(160 / 8); + ::SHA1(reinterpret_cast(&input[0]), input.size(), reinterpret_cast(&hash[0])); + + for (size_t c = 1; c < iterations; ++c) + ::SHA1(reinterpret_cast(&hash[0]), hash.size(), reinterpret_cast(&hash[0])); - hash.resize(160/8); - SHA1_Final((unsigned char*)&hash[0], &context); - } - template - type SHA1(const type& input) { - type hash; - SHA1(input, hash); return hash; } - template - void SHA256(const type& input, type& hash) { - SHA256_CTX context; - SHA256_Init(&context); - SHA256_Update(&context, &input[0], input.size()); + std::string SHA256(const std::string &input, size_t iterations=1) { + std::string hash; + + hash.resize(256 / 8); + ::SHA256(reinterpret_cast(&input[0]), input.size(), reinterpret_cast(&hash[0])); + + for (size_t c = 1; c < iterations; ++c) + ::SHA256(reinterpret_cast(&hash[0]), hash.size(), reinterpret_cast(&hash[0])); - hash.resize(256/8); - SHA256_Final((unsigned char*)&hash[0], &context); - } - template - type SHA256(const type& input) { - type hash; - SHA256(input, hash); return hash; } - template - void SHA512(const type& input, type& hash) { - SHA512_CTX context; - SHA512_Init(&context); - SHA512_Update(&context, &input[0], input.size()); + std::string SHA512(const std::string &input, size_t iterations=1) { + std::string hash; + + hash.resize(512 / 8); + ::SHA512(reinterpret_cast(&input[0]), input.size(), reinterpret_cast(&hash[0])); + + for (size_t c = 1; c < iterations; ++c) + ::SHA512(reinterpret_cast(&hash[0]), hash.size(), reinterpret_cast(&hash[0])); - hash.resize(512/8); - SHA512_Final((unsigned char*)&hash[0], &context); - } - template - type SHA512(const type& input) { - type hash; - SHA512(input, hash); return hash; } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 77c91235..531156ee 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,6 +11,7 @@ target_link_libraries(io_test ${CMAKE_THREAD_LIBS_INIT}) add_executable(parse_test parse_test.cpp) target_link_libraries(parse_test ${Boost_LIBRARIES}) +target_link_libraries(parse_test ${OPENSSL_CRYPTO_LIBRARY}) target_link_libraries(parse_test ${CMAKE_THREAD_LIBS_INIT}) if(MSYS) diff --git a/tests/crypto_test.cpp b/tests/crypto_test.cpp index bd0b0c64..30e7e902 100644 --- a/tests/crypto_test.cpp +++ b/tests/crypto_test.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include "crypto.hpp" @@ -39,12 +39,11 @@ const vector > SHA512_string_tests = { {"The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"} }; -template -string to_hex_string(type chars) { +string hex_string(const std::string &chars) { stringstream hex_ss; hex_ss.fill('0'); for(auto c: chars) { - hex_ss << setw(2) << hex << (int)(unsigned char)c; + hex_ss << setw(2) << hex << static_cast(static_cast(c)); } return hex_ss.str(); } @@ -52,87 +51,28 @@ string to_hex_string(type chars) { int main() { //Testing SimpleWeb::Crypt::Base64 for(auto& string_test: Base64_string_tests) { - if(Crypto::Base64::encode(string_test.first)!=string_test.second) { - cerr << "FAIL Crypto::Base64::encode: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - if(Crypto::Base64::decode(string_test.second)!=string_test.first) { - cerr << "FAIL Crypto::Base64::decode: " << string_test.second << "!=" << string_test.first << endl; - return 1; - } - - pair, vector > vector_test={ - {string_test.first.begin(), string_test.first.end()}, - {string_test.second.begin(), string_test.second.end()} - }; - if(Crypto::Base64::encode(vector_test.first)!=vector_test.second) { - cerr << "FAIL Crypto::Base64::encode: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - if(Crypto::Base64::decode(vector_test.second)!=vector_test.first) { - cerr << "FAIL Crypto::Base64::decode: " << string_test.second << "!=" << string_test.first << endl; - return 1; - } + 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) { - if(to_hex_string(Crypto::MD5(string_test.first)) != string_test.second) { - cerr << "FAIL Crypto::MD5: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - - vector vector_test_first(string_test.first.begin(), string_test.first.end()); - if(to_hex_string(Crypto::MD5(vector_test_first)) != string_test.second) { - cerr << "FAIL Crypto::MD5: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - } + for(auto& string_test: MD5_string_tests) + assert(hex_string(Crypto::MD5(string_test.first)) == string_test.second); //Testing SimpleWeb::Crypt::SHA1 - for(auto& string_test: SHA1_string_tests) { - if(to_hex_string(Crypto::SHA1(string_test.first)) != string_test.second) { - cerr << "FAIL Crypto::SHA1: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - - vector vector_test_first(string_test.first.begin(), string_test.first.end()); - if(to_hex_string(Crypto::SHA1(vector_test_first)) != string_test.second) { - cerr << "FAIL Crypto::SHA1: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - } + for(auto& string_test: SHA1_string_tests) + assert(hex_string(Crypto::SHA1(string_test.first)) == string_test.second); //Testing SimpleWeb::Crypt::SHA256 - for(auto& string_test: SHA256_string_tests) { - if(to_hex_string(Crypto::SHA256(string_test.first)) != string_test.second) { - cerr << "FAIL Crypto::SHA256: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - - vector vector_test_first(string_test.first.begin(), string_test.first.end()); - if(to_hex_string(Crypto::SHA256(vector_test_first)) != string_test.second) { - cerr << "FAIL Crypto::SHA256: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - } + for(auto& string_test: SHA256_string_tests) + assert(hex_string(Crypto::SHA256(string_test.first)) == string_test.second); //Testing SimpleWeb::Crypt::SHA512 - for(auto& string_test: SHA512_string_tests) { - if(to_hex_string(Crypto::SHA512(string_test.first)) != string_test.second) { - cerr << "FAIL Crypto::SHA512: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - - vector vector_test_first(string_test.first.begin(), string_test.first.end()); - if(to_hex_string(Crypto::SHA512(vector_test_first)) != string_test.second) { - cerr << "FAIL Crypto::SHA512: " << string_test.first << "!=" << string_test.second << endl; - return 1; - } - } + for(auto& string_test: SHA512_string_tests) + assert(hex_string(Crypto::SHA512(string_test.first)) == string_test.second); - cout << "PASS" << endl; - - return 0; + //Testing iterations + assert(hex_string(Crypto::SHA1("Test", 1)) == "640ab2bae07bedc4c163f679a746f7ab7fb5d1fa"); + assert(hex_string(Crypto::SHA1("Test", 2)) == "af31c6cbdecd88726d0a9b3798c71ef41f1624d5"); }