Replace base64-implementation with hex-encoding (temporary)

This commit is contained in:
G.H.O.S.T 2024-12-31 00:56:39 +01:00
parent 4216469674
commit f0c0cc284a
Signed by: G.H.O.S.T
GPG Key ID: 3BD93EABD1407B82

View File

@ -4,7 +4,8 @@
#include <openssl/buffer.h> #include <openssl/buffer.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <cstring> #include <iomanip>
#include <sstream>
namespace code::build::security::base64 namespace code::build::security::base64
{ {
@ -12,6 +13,15 @@ namespace code::build::security::base64
string string
encode(string const& input) 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* base64{};
BIO* bio{}; BIO* bio{};
string output; string output;
@ -29,8 +39,10 @@ namespace code::build::security::base64
BUF_MEM* buffer_ptr{}; BUF_MEM* buffer_ptr{};
BIO_get_mem_ptr(bio, &buffer_ptr); BIO_get_mem_ptr(bio, &buffer_ptr);
output.resize(buffer_ptr->length); output = string{buffer_ptr->data, buffer_ptr->data + buffer_ptr->length};
std::memcpy(output.data(), buffer_ptr->data, output.size());
// output.resize(buffer_ptr->length);
// std::memcpy(output.data(), buffer_ptr->data, output.size());
BIO_free_all(bio); BIO_free_all(bio);
} }
@ -38,13 +50,26 @@ namespace code::build::security::base64
BIO_free_all(bio); BIO_free_all(bio);
throw; throw;
} }
#endif
return output;
} }
string string
decode(string const& input) 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* base64{};
BIO* bio{}; BIO* bio{};
string output; string output;
@ -68,8 +93,7 @@ namespace code::build::security::base64
BIO_free_all(bio); BIO_free_all(bio);
throw; throw;
} }
#endif
return output;
} }
} // namespace code::build::security::base64 } // namespace code::build::security::base64