miércoles, febrero 26, 2014

OpenSSL Base64 encoding

I'll just leave this here in case anyone else is interested:


#include <openssl/bio.h>
#include <openssl/evp.h>
void base64calc(const unsigned char* msg, size_t msgsize, char* out, size_t outsize) {
BIO *bio, *b64, *mem;
// Shit, this base64 thing with openssl was hard to get right.
b64 = BIO_new(BIO_f_base64());
// No b64 newlines.
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
mem = BIO_new(BIO_s_mem());
bio = BIO_push(b64, mem);
BIO_write(bio, msg, msgsize);
BIO_flush(bio);
BIO_gets(mem, out, outsize);
BIO_free_all(bio);
}