Skip to content

Instantly share code, notes, and snippets.

@alepez
Created January 21, 2016 16:14
Show Gist options
  • Save alepez/0ad63a6e32d45df024e5 to your computer and use it in GitHub Desktop.
Save alepez/0ad63a6e32d45df024e5 to your computer and use it in GitHub Desktop.
c++ base64 RAII
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
using RawData = std::vector<uint8_t>;
RawData encode(const RawData& input) {
BIO* b64 = BIO_new(BIO_f_base64());
BIO* bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, input.data(), input.size());
BIO_flush(bio);
BUF_MEM* buf;
BIO_get_mem_ptr(bio, &buf);
RawData output(buf->length);
std::copy(buf->data, buf->data + buf->length, output.begin());
BIO_set_close(bio, BIO_NOCLOSE);
BIO_free_all(bio);
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment