diff --git a/code/build/security/base64.cxx b/code/build/security/base64.cxx index f41a935..c82bc37 100644 --- a/code/build/security/base64.cxx +++ b/code/build/security/base64.cxx @@ -4,7 +4,8 @@ #include #include -#include +#include +#include namespace code::build::security::base64 { @@ -12,6 +13,15 @@ namespace code::build::security::base64 string encode(string const& input) { + std::ostringstream o; + + for (unsigned char c : input) { + o << std::setw(2) << std::setfill('0') << std::hex << (int)c; + } + + return o.str(); + +#if 0 BIO* base64{}; BIO* bio{}; string output; @@ -29,8 +39,10 @@ namespace code::build::security::base64 BUF_MEM* buffer_ptr{}; BIO_get_mem_ptr(bio, &buffer_ptr); - output.resize(buffer_ptr->length); - std::memcpy(output.data(), buffer_ptr->data, output.size()); + output = string{buffer_ptr->data, buffer_ptr->data + buffer_ptr->length}; + +// output.resize(buffer_ptr->length); +// std::memcpy(output.data(), buffer_ptr->data, output.size()); BIO_free_all(bio); } @@ -38,13 +50,26 @@ namespace code::build::security::base64 BIO_free_all(bio); throw; } - - return output; +#endif } string decode(string const& input) { + if (input.length() % 2 != 0) { + throw std::invalid_argument("Invalid hex string length"); + } + + std::string output; + for (size_t i = 0; i < input.length(); i += 2) { + std::string byte_str = input.substr(i, 2); + unsigned char byte = std::stoi(byte_str, nullptr, 16); + output.push_back(byte); + } + + return output; + +#if 0 BIO* base64{}; BIO* bio{}; string output; @@ -68,8 +93,7 @@ namespace code::build::security::base64 BIO_free_all(bio); throw; } - - return output; +#endif } } // namespace code::build::security::base64