openssl crypto library - base64 conversion
- by Hassan Syed
I'm using openssl BIO objects to convert a binary string into a base64 string. The code is as follows:
void ToBase64(std::string & s_in) {
BIO * b_s = BIO_new( BIO_s_mem() );
BIO * b64_f = BIO_new( BIO_f_base64() );
b_s = BIO_push( b64_f , b_s);
std::cout << "IN::" << s_in.length();
BIO_write(b_s, s_in.c_str(), s_in.length());
char * pp;
int sz = BIO_get_mem_data(b_s, &pp);
std::cout << "OUT::" << sz << endl;
s_in.assign(pp,sz);
//std::cout << sz << " " << std::string(pp,sz) << std::endl;
BIO_free (b64_f); // TODO ret error potential
BIO_free (b_s); //
}
The in length is either 64 or 72. However the output is always 65, which is incorrect it should be much larger than that. The documentation isn't the best in the world, AFAIK the bio_s_mem object is supposed to grow dynamically. What am I doing wrong ?