#include #include #include #include #include "utils.h" std::string utils::to_base64(const std::vector& binary) { using namespace boost::archive::iterators; using It = base64_from_binary::const_iterator, 6, 8>>; auto base64 = std::string(It(binary.begin()), It(binary.end())); // Add padding. return base64.append((3 - binary.size() % 3) % 3, '='); } std::vector utils::from_base64(const std::string& base64) { using namespace boost::archive::iterators; using It = transform_width, 8, 6>; auto binary = std::vector(It(base64.begin()), It(base64.end())); // Remove padding. auto length = base64.size(); if(binary.size() > 2 && base64[length - 1] == '=' && base64[length - 2] == '=') { binary.erase(binary.end() - 2, binary.end()); } else if(binary.size() > 1 && base64[length - 1] == '=') { binary.erase(binary.end() - 1, binary.end()); } return binary; } std::vector utils::generate_random(const size_t size){ std::vector res(size); if(!RAND_bytes(res.data(), size)){ throw std::exception(); } return res; }