diff options
author | Sergey Yershov <syershov@maps.me> | 2016-11-16 17:23:55 +0300 |
---|---|---|
committer | Sergey Yershov <syershov@maps.me> | 2016-11-17 13:27:23 +0300 |
commit | d9e4a1b1a28fc888ca0b1a333c6a1b49a52fb6ce (patch) | |
tree | 31a563d73b7562082b4f26e2b2ed05e70ccc0a18 /coding | |
parent | 19600faf61beb3a36b02ccb458eaffa0a39edb17 (diff) |
Remove dependencies on tomcrypt
Diffstat (limited to 'coding')
-rw-r--r-- | coding/CMakeLists.txt | 2 | ||||
-rw-r--r-- | coding/base64.cpp | 81 | ||||
-rw-r--r-- | coding/base64.hpp | 15 | ||||
-rw-r--r-- | coding/coding.pro | 2 | ||||
-rw-r--r-- | coding/coding_tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | coding/coding_tests/base64_for_user_id_test.cpp | 46 | ||||
-rw-r--r-- | coding/coding_tests/coding_tests.pro | 2 | ||||
-rw-r--r-- | coding/coding_tests/sha2_test.cpp | 15 | ||||
-rw-r--r-- | coding/sha2.cpp | 25 | ||||
-rw-r--r-- | coding/sha2.hpp | 30 |
10 files changed, 14 insertions, 206 deletions
diff --git a/coding/CMakeLists.txt b/coding/CMakeLists.txt index 5a912d765d..b4a6f46bec 100644 --- a/coding/CMakeLists.txt +++ b/coding/CMakeLists.txt @@ -56,8 +56,6 @@ set( reader_wrapper.hpp reader_writer_ops.cpp reader_writer_ops.hpp - sha2.cpp - sha2.hpp simple_dense_coding.cpp simple_dense_coding.hpp streams.hpp diff --git a/coding/base64.cpp b/coding/base64.cpp index 3ad61ebd6e..e370f5332f 100644 --- a/coding/base64.cpp +++ b/coding/base64.cpp @@ -1,85 +1,30 @@ #include "coding/base64.hpp" -#include "3party/tomcrypt/src/headers/tomcrypt.h" -#include "3party/tomcrypt/src/headers/tomcrypt_misc.h" -#include "base/assert.hpp" - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wreorder" +#include <boost/algorithm/string.hpp> #include <boost/archive/iterators/base64_from_binary.hpp> #include <boost/archive/iterators/binary_from_base64.hpp> #include <boost/archive/iterators/transform_width.hpp> #pragma GCC diagnostic pop -using namespace boost::archive::iterators; -typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > base64_t; -typedef transform_width<binary_from_base64<string::const_iterator>, 8, 6 > binary_t; - -/// This namespace contains historically invalid implementation of base64, -/// but it's still needed for production code -namespace base64_for_user_ids -{ - string encode(string rawBytes) - { - // http://boost.2283326.n4.nabble.com/boost-archive-iterators-base64-from-binary-td2589980.html - switch (rawBytes.size() % 3) - { - case 1: - rawBytes.push_back('0'); - case 2: - rawBytes.push_back('0'); - break; - } - - return string(base64_t(rawBytes.begin()), base64_t(rawBytes.end())); - } - - string decode(string const & base64Chars) - { - if (base64Chars.empty()) - return string(); - // minus 1 needed to avoid finishing zero in a string - return string(binary_t(base64Chars.begin()), binary_t(base64Chars.begin() + base64Chars.size() - 1)); - } -} - namespace base64 { +// From: http://stackoverflow.com/a/28471421 -string Encode(string const & bytesToEncode) +std::string Decode(const std::string & val) { - string result; - long unsigned int resSize = (bytesToEncode.size() + 3) * 4 / 3; - // Raw new/delete because we don't throw exceptions inbetween - unsigned char * buffer = new unsigned char[resSize]; - if (CRYPT_OK == base64_encode(reinterpret_cast<unsigned char const *>(bytesToEncode.data()), - bytesToEncode.size(), - buffer, - &resSize)) - result.assign(reinterpret_cast<char const *>(buffer), resSize); - else - ASSERT(false, ("It should work!")); - - delete[] buffer; - return result; + using namespace boost::archive::iterators; + using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>; + return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), + [](char c) { return c == '\0'; }); } -string Decode(string const & base64CharsToDecode) +std::string Encode(const std::string & val) { - string result; - long unsigned int resSize = base64CharsToDecode.size() * 3 / 4 + 2; - // Raw new/delete because we don't throw exceptions inbetween - unsigned char * buffer = new unsigned char[resSize]; - if (CRYPT_OK == base64_decode(reinterpret_cast<unsigned char const *>(base64CharsToDecode.data()), - base64CharsToDecode.size(), - buffer, - &resSize)) - result.assign(reinterpret_cast<char const *>(buffer), resSize); - else - ASSERT(false, ("It should work!")); - - delete[] buffer; - return result; -} - + using namespace boost::archive::iterators; + using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>; + auto tmp = std::string(It(std::begin(val)), It(std::end(val))); + return tmp.append((3 - val.size() % 3) % 3, '='); } +} // namespace base64 diff --git a/coding/base64.hpp b/coding/base64.hpp index c73f24ed40..80f6d540df 100644 --- a/coding/base64.hpp +++ b/coding/base64.hpp @@ -1,22 +1,9 @@ #pragma once -#include "std/vector.hpp" #include "std/string.hpp" namespace base64 { - string Encode(string const & bytesToEncode); string Decode(string const & base64CharsToDecode); - -} - -/// This namespace contains historically invalid implementation of base64, -/// but it's still needed for production code -namespace base64_for_user_ids -{ - -string encode(string rawBytes); -string decode(string const & base64Chars); - -} +} // namespace base64 diff --git a/coding/coding.pro b/coding/coding.pro index 2ab0b4cbaa..a988573c3f 100644 --- a/coding/coding.pro +++ b/coding/coding.pro @@ -27,7 +27,6 @@ SOURCES += \ reader_streambuf.cpp \ reader_writer_ops.cpp \ simple_dense_coding.cpp \ - sha2.cpp \ traffic.cpp \ uri.cpp \ # varint_vector.cpp \ @@ -79,7 +78,6 @@ HEADERS += \ reader_wrapper.hpp \ reader_writer_ops.hpp \ simple_dense_coding.hpp \ - sha2.hpp \ streams.hpp \ streams_common.hpp \ streams_sink.hpp \ diff --git a/coding/coding_tests/CMakeLists.txt b/coding/coding_tests/CMakeLists.txt index 236728c9da..9872c8460d 100644 --- a/coding/coding_tests/CMakeLists.txt +++ b/coding/coding_tests/CMakeLists.txt @@ -4,7 +4,6 @@ add_definitions(-DOMIM_UNIT_TEST_DISABLE_PLATFORM_INIT) set( SRC - base64_for_user_id_test.cpp base64_test.cpp bit_streams_test.cpp coder_test.hpp @@ -29,7 +28,6 @@ set( reader_test.cpp reader_test.hpp reader_writer_ops_test.cpp - sha2_test.cpp simple_dense_coding_test.cpp succinct_mapper_test.cpp traffic_test.cpp diff --git a/coding/coding_tests/base64_for_user_id_test.cpp b/coding/coding_tests/base64_for_user_id_test.cpp deleted file mode 100644 index c9d8d268ea..0000000000 --- a/coding/coding_tests/base64_for_user_id_test.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "testing/testing.hpp" - -#include "coding/base64.hpp" - -#include "base/logging.hpp" - -#include "std/random.hpp" - -using namespace base64_for_user_ids; - -UNIT_TEST(Base64_Encode_User_Ids) -{ - TEST_EQUAL(encode("Hello, world!"), "SGVsbG8sIHdvcmxkITAw", ()); - TEST_EQUAL(encode(""), "", ()); - TEST_EQUAL(encode("$"), "JDAw", ()); - TEST_EQUAL(encode("MapsWithMe is an offline maps application for any device in the world."), - "TWFwc1dpdGhNZSBpcyBhbiBvZmZsaW5lIG1hcHMgYXBwbGljYXRpb24gZm9yIGFueSBkZXZpY2Ug" - "aW4gdGhlIHdvcmxkLjAw", ()); -} - -// Removed decode test, because boost behavior is undefined, and we don't actually need -// to decode these oldscool ids in our own code. Our new code should use base64::Encode/Decode instead - -UNIT_TEST(Base64_QualityTest_User_Ids) -{ - size_t const NUMBER_OF_TESTS = 10000; - mt19937 rng(0); - for (size_t i = 0; i < NUMBER_OF_TESTS; ++i) - { - string randomBytes; - for (size_t j = 0 ; j < 8; ++j) - { - if (j == 4) - { - randomBytes.push_back('\0'); - continue; - } - randomBytes.push_back(static_cast<char>(rng())); - } - string const result = encode(randomBytes); - TEST_GREATER_OR_EQUAL(result.size(), randomBytes.size(), - (randomBytes, result)); - for (size_t i = 0; i < result.size(); ++i) - TEST_NOT_EQUAL(result[i], 0, ()); - } -} diff --git a/coding/coding_tests/coding_tests.pro b/coding/coding_tests/coding_tests.pro index dce6ba1e49..ad8bc42da0 100644 --- a/coding/coding_tests/coding_tests.pro +++ b/coding/coding_tests/coding_tests.pro @@ -13,7 +13,6 @@ include($$ROOT_DIR/common.pri) DEFINES += OMIM_UNIT_TEST_DISABLE_PLATFORM_INIT SOURCES += ../../testing/testingmain.cpp \ - base64_for_user_id_test.cpp \ base64_test.cpp \ bit_streams_test.cpp \ coder_util_test.cpp \ @@ -37,7 +36,6 @@ SOURCES += ../../testing/testingmain.cpp \ reader_test.cpp \ reader_writer_ops_test.cpp \ simple_dense_coding_test.cpp \ - sha2_test.cpp \ succinct_mapper_test.cpp \ traffic_test.cpp \ uri_test.cpp \ diff --git a/coding/coding_tests/sha2_test.cpp b/coding/coding_tests/sha2_test.cpp deleted file mode 100644 index 4fdc192f79..0000000000 --- a/coding/coding_tests/sha2_test.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "base/SRC_FIRST.hpp" -#include "testing/testing.hpp" - -#include "coding/sha2.hpp" - -UNIT_TEST(Sha2_256) -{ - TEST_EQUAL(sha2::digest256("Hello, world!"), - "315F5BDB76D078C43B8AC0064E4A0164612B1FCE77C869345BFC94C75894EDD3", ()); - TEST_EQUAL(sha2::digest256(""), - "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855", ()); - char const zero[] = "\x3e\x23\xe8\x16\x00\x39\x59\x4a\x33\x89\x4f\x65\x64\xe1\xb1\x34\x8b\xbd\x7a\x00\x88\xd4\x2c\x4a\xcb\x73\xee\xae\xd5\x9c\x00\x9d"; - TEST_EQUAL(sha2::digest256("b", false), - string(zero, ARRAY_SIZE(zero) - 1), ()); -} diff --git a/coding/sha2.cpp b/coding/sha2.cpp deleted file mode 100644 index 67f2dc199d..0000000000 --- a/coding/sha2.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "coding/sha2.hpp" -#include "coding/hex.hpp" - -#include "base/macros.hpp" - -#include "3party/tomcrypt/src/headers/tomcrypt.h" - -namespace sha2 -{ - string digest256(char const * data, size_t dataSize, bool returnAsHexString) - { - hash_state md; - unsigned char out[256/8] = { 0 }; - if (CRYPT_OK == sha256_init(&md) - && CRYPT_OK == sha256_process(&md, reinterpret_cast<unsigned char const *>(data), dataSize) - && CRYPT_OK == sha256_done(&md, out)) - { - if (returnAsHexString) - return ToHex(string(reinterpret_cast<char const *>(out), ARRAY_SIZE(out))); - else - return string(reinterpret_cast<char const *>(out), ARRAY_SIZE(out)); - } - return string(); - } -} diff --git a/coding/sha2.hpp b/coding/sha2.hpp deleted file mode 100644 index 80b8cf1d40..0000000000 --- a/coding/sha2.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include "std/string.hpp" - -namespace sha2 -{ - string digest224(char const * data, size_t dataSize, bool returnAsHexString); - inline string digest224(string const & data, bool returnAsHexString = true) - { - return digest224(data.c_str(), data.size(), returnAsHexString); - } - - string digest256(char const * data, size_t dataSize, bool returnAsHexString); - inline string digest256(string const & data, bool returnAsHexString = true) - { - return digest256(data.c_str(), data.size(), returnAsHexString); - } - - string digest384(char const * data, size_t dataSize, bool returnAsHexString); - inline string digest384(string const & data, bool returnAsHexString = true) - { - return digest384(data.c_str(), data.size(), returnAsHexString); - } - - string digest512(char const * data, size_t dataSize, bool returnAsHexString); - inline string digest512(string const & data, bool returnAsHexString = true) - { - return digest512(data.c_str(), data.size(), returnAsHexString); - } -} |