Created
April 20, 2022 08:14
Revisions
-
tanbro created this gist
Apr 20, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ #include <stddef.h> #include <memory.h> #include <openssl/bio.h> #include <openssl/buffer.h> #include <openssl/evp.h> /// 参考: https://www.openssl.org/docs/man1.0.2/man3/BIO_f_base64.html 以及 /// https://gist.github.com/barrysteyn/7308212 void b64_encode_s(const unsigned char *in_data, size_t in_len, char **out_str) { BIO_s_mem(); BIO *b64 = BIO_new(BIO_f_base64()); BIO *bio = BIO_new(BIO_s_mem()); BIO_set_close(bio, BIO_NOCLOSE); BIO_push(b64, bio); BIO_write(b64, in_data, in_len); BIO_flush(b64); BUF_MEM *pbuf; BIO_get_mem_ptr(bio, &(pbuf)); *out_str = (char *)calloc(pbuf->length + 1, sizeof(char)); memcpy(*out_str, pbuf->data, pbuf->length); BIO_free_all(bio); } void b64_encode_b(const unsigned char *in_data, size_t in_len, unsigned char **out_data, size_t *out_len) { BIO_s_mem(); BIO *b64 = BIO_new(BIO_f_base64()); BIO *bio = BIO_new(BIO_s_mem()); BIO_push(b64, bio); BIO_write(b64, in_data, in_len); BIO_flush(b64); BIO_set_close(bio, BIO_NOCLOSE); BUF_MEM *pbuf; BIO_get_mem_ptr(bio, &(pbuf)); *out_data = (unsigned char *)calloc(pbuf->length, sizeof(unsigned char)); memcpy(*out_data, pbuf->data, pbuf->length); if (out_len) *out_len = pbuf->length; BIO_free_all(bio); }