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/base64.cpp | |
parent | 19600faf61beb3a36b02ccb458eaffa0a39edb17 (diff) |
Remove dependencies on tomcrypt
Diffstat (limited to 'coding/base64.cpp')
-rw-r--r-- | coding/base64.cpp | 81 |
1 files changed, 13 insertions, 68 deletions
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 |