Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Crypto cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eidheim committed Dec 6, 2016
1 parent 3821ece commit 9cdef90
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 140 deletions.
104 changes: 40 additions & 64 deletions crypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace SimpleWeb {
}
#endif

//type must support size(), resize() and operator[]
namespace Crypto {
namespace Base64 {
template<class type>
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();

Expand All @@ -48,16 +48,13 @@ namespace SimpleWeb {
bptr->data=nullptr;

BIO_free_all(b64);
}
template<class type>
type encode(const type& ascii) {
type base64;
encode(ascii, base64);

return base64;
}

template<class type>
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;
Expand All @@ -71,77 +68,56 @@ namespace SimpleWeb {
ascii.resize(decoded_length);

BIO_free_all(b64);
}
template<class type>
type decode(const type& base64) {
type ascii;
decode(base64, ascii);

return ascii;
}

}

template<class type>
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<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));

for (size_t c = 1; c < iterations; ++c)
::MD5(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));

hash.resize(128/8);
MD5_Final((unsigned char*)&hash[0], &context);
}
template<class type>
type MD5(const type& input) {
type hash;
MD5(input, hash);
return hash;
}

template<class type>
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<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));

for (size_t c = 1; c < iterations; ++c)
::SHA1(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));

hash.resize(160/8);
SHA1_Final((unsigned char*)&hash[0], &context);
}
template<class type>
type SHA1(const type& input) {
type hash;
SHA1(input, hash);
return hash;
}

template<class type>
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<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));

for (size_t c = 1; c < iterations; ++c)
::SHA256(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));

hash.resize(256/8);
SHA256_Final((unsigned char*)&hash[0], &context);
}
template<class type>
type SHA256(const type& input) {
type hash;
SHA256(input, hash);
return hash;
}

template<class type>
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<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));

for (size_t c = 1; c < iterations; ++c)
::SHA512(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));

hash.resize(512/8);
SHA512_Final((unsigned char*)&hash[0], &context);
}
template<class type>
type SHA512(const type& input) {
type hash;
SHA512(input, hash);
return hash;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
92 changes: 16 additions & 76 deletions tests/crypto_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include <iomanip>
#include <cassert>

#include "crypto.hpp"

Expand Down Expand Up @@ -39,100 +39,40 @@ const vector<pair<string, string> > SHA512_string_tests = {
{"The quick brown fox jumps over the lazy dog", "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"}
};

template<class type>
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<int>(static_cast<unsigned char>(c));
}
return hex_ss.str();
}

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<unsigned char>, vector<unsigned char> > 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<unsigned char> 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<unsigned char> 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<unsigned char> 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<unsigned char> 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");
}

0 comments on commit 9cdef90

Please sign in to comment.