인코딩
string encodeBase64(const unsigned char *buf, size_t blen)
{
auto plen = 4 * ((blen + 2) / 3);
auto obuf = make_unique<unsigned char[]>(plen + 1);
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
EVP_EncodeInit(ctx);
int olen = plen;
EVP_EncodeUpdate(ctx, obuf.get(), &olen, buf, blen);
int flen = 0;
EVP_EncodeFinal(ctx, obuf.get() + olen, &flen);
olen += flen;
EVP_ENCODE_CTX_free(ctx);
return string((char *)obuf.get(), olen);
}
디코딩
pair<unique_ptr<unsigned char[]>, size_t> decodeBase64(const string &buf)
{
auto plen = 3 * buf.size() / 4;
auto obuf = make_unique<unsigned char[]>(plen + 1);
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
EVP_DecodeInit(ctx);
int olen = plen;
EVP_DecodeUpdate(ctx, obuf.get(), &olen, (unsigned char *)buf.c_str(), buf.size());
int flen = 0;
EVP_DecodeFinal(ctx, obuf.get() + olen, &flen);
olen += flen;
EVP_ENCODE_CTX_free(ctx);
return make_pair<unique_ptr<unsigned char[]>, size_t>(move(obuf), olen);
}
댓글
댓글 쓰기