|
1 | 1 | #include <bytes/bytes.h> |
2 | 2 |
|
| 3 | +#include <array> |
3 | 4 | #include <iomanip> |
4 | | -#include <iostream> |
| 5 | +#include <openssl/bio.h> |
| 6 | +#include <openssl/err.h> |
| 7 | +#include <openssl/evp.h> |
5 | 8 | #include <sstream> |
6 | 9 | #include <stdexcept> |
7 | 10 |
|
@@ -137,4 +140,123 @@ operator!=(const std::vector<uint8_t>& lhs, const bytes_ns::bytes& rhs) |
137 | 140 | return rhs != lhs; |
138 | 141 | } |
139 | 142 |
|
| 143 | +std::string |
| 144 | +to_base64(const bytes& data) |
| 145 | +{ |
| 146 | + bool done = false; |
| 147 | + int result = 0; |
| 148 | + |
| 149 | + if (data.empty()) { |
| 150 | + return ""; |
| 151 | + } |
| 152 | + |
| 153 | + BIO* b64 = BIO_new(BIO_f_base64()); |
| 154 | + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); |
| 155 | + BIO* out = BIO_new(BIO_s_mem()); |
| 156 | + BIO_push(b64, out); |
| 157 | + |
| 158 | + while (!done) { |
| 159 | + result = BIO_write(b64, data.data(), static_cast<int>(data.size())); |
| 160 | + |
| 161 | + if (result <= 0) { |
| 162 | + if (BIO_should_retry(b64)) { |
| 163 | + continue; |
| 164 | + } |
| 165 | + throw std::runtime_error("base64 encode failed"); |
| 166 | + } |
| 167 | + done = true; |
| 168 | + } |
| 169 | + BIO_flush(b64); |
| 170 | + char* string_ptr = nullptr; |
| 171 | + // long string_len = BIO_get_mem_data(out, &string_ptr); |
| 172 | + // BIO_get_mem_data failed clang-tidy |
| 173 | + long string_len = BIO_ctrl(out, BIO_CTRL_INFO, 0, &string_ptr); |
| 174 | + auto return_value = std::string(string_ptr, string_len); |
| 175 | + |
| 176 | + BIO_set_close(out, BIO_NOCLOSE); |
| 177 | + BIO_free(b64); |
| 178 | + BIO_free(out); |
| 179 | + return return_value; |
| 180 | +} |
| 181 | + |
| 182 | +std::string |
| 183 | +to_base64url(const bytes& data) |
| 184 | +{ |
| 185 | + if (data.empty()) { |
| 186 | + return ""; |
| 187 | + } |
| 188 | + |
| 189 | + std::string return_value = to_base64(data); |
| 190 | + |
| 191 | + // remove the end padding |
| 192 | + auto sz = return_value.find_first_of('='); |
| 193 | + |
| 194 | + if (sz != std::string::npos) { |
| 195 | + return_value = return_value.substr(0, sz); |
| 196 | + } |
| 197 | + |
| 198 | + // replace plus with hyphen |
| 199 | + std::replace(return_value.begin(), return_value.end(), '+', '-'); |
| 200 | + |
| 201 | + // replace slash with underscore |
| 202 | + std::replace(return_value.begin(), return_value.end(), '/', '_'); |
| 203 | + return return_value; |
| 204 | +} |
| 205 | + |
| 206 | +bytes |
| 207 | +from_base64(const std::string& enc) |
| 208 | +{ |
| 209 | + if (enc.length() == 0) { |
| 210 | + return {}; |
| 211 | + } |
| 212 | + |
| 213 | + if (enc.length() % 4 != 0) { |
| 214 | + throw std::runtime_error("Base64 length is not divisible by 4"); |
| 215 | + } |
| 216 | + bytes input = from_ascii(enc); |
| 217 | + bytes output(input.size() / 4 * 3); |
| 218 | + int output_buffer_length = static_cast<int>(output.size()); |
| 219 | + EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new(); |
| 220 | + EVP_DecodeInit(ctx); |
| 221 | + |
| 222 | + int result = EVP_DecodeUpdate(ctx, |
| 223 | + output.data(), |
| 224 | + &output_buffer_length, |
| 225 | + input.data(), |
| 226 | + static_cast<int>(input.size())); |
| 227 | + |
| 228 | + if (result == -1) { |
| 229 | + auto code = ERR_get_error(); |
| 230 | + throw std::runtime_error(ERR_error_string(code, nullptr)); |
| 231 | + } |
| 232 | + |
| 233 | + if (result == 0 && enc.substr(enc.length() - 2, enc.length()) == "==") { |
| 234 | + output = output.slice(0, output.size() - 2); |
| 235 | + } else if (result == 0 && enc.substr(enc.length() - 1, enc.length()) == "=") { |
| 236 | + output = output.slice(0, output.size() - 1); |
| 237 | + } else if (result == 0) { |
| 238 | + throw std::runtime_error("Base64 padding was malformed."); |
| 239 | + } |
| 240 | + EVP_DecodeFinal(ctx, output.data(), &output_buffer_length); |
| 241 | + EVP_ENCODE_CTX_free(ctx); |
| 242 | + return output; |
| 243 | +} |
| 244 | + |
| 245 | +bytes |
| 246 | +from_base64url(const std::string& enc) |
| 247 | +{ |
| 248 | + if (enc.empty()) { |
| 249 | + return {}; |
| 250 | + } |
| 251 | + std::string enc_copy = enc; // copy |
| 252 | + std::replace(enc_copy.begin(), enc_copy.end(), '-', '+'); |
| 253 | + std::replace(enc_copy.begin(), enc_copy.end(), '_', '/'); |
| 254 | + |
| 255 | + while (enc_copy.length() % 4 != 0) { |
| 256 | + enc_copy += "="; |
| 257 | + } |
| 258 | + bytes return_value = from_base64(enc_copy); |
| 259 | + return return_value; |
| 260 | +} |
| 261 | + |
140 | 262 | } // namespace bytes_ns |
0 commit comments