XROAD
xroad_base64.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <openssl/hmac.h>
9 #include <openssl/bio.h>
10 #include <openssl/sha.h>
11 #include <openssl/evp.h>
12 #include <openssl/buffer.h>
13 
14 #ifdef __cplusplus
15 extern "C"
16 {
17 #endif
18 
19 #define xroad_base64_encode(_data, _len) \
20 ({ \
21  BIO* base64 = BIO_new(BIO_f_base64()); \
22  BIO* b_mem = BIO_new(BIO_s_mem()); \
23  BIO* all = BIO_push(base64, b_mem); \
24  BIO_set_flags(all, BIO_FLAGS_BASE64_NO_NL); \
25  BIO_write(all, (_data), (_len)); \
26  (void)BIO_flush(all); \
27  BUF_MEM *bptr; \
28  BIO_get_mem_ptr(all, &bptr); \
29  xroad_str_t ret = xroad_str_dupa(xroad_str_len(bptr->data, bptr->length)); \
30  BIO_free_all(all); \
31  ret; \
32 })
33 
34 #define xroad_base64_decode(_str) \
35 ({ \
36  char* ret = alloca(_str.len); \
37  BIO* str64 = BIO_new_mem_buf(_str.data, _str.len); \
38  BIO* base64 = BIO_new(BIO_f_base64()); \
39  BIO* all = BIO_push(base64, str64); \
40  BIO_set_flags(all, BIO_FLAGS_BASE64_NO_NL); \
41  size_t len = BIO_read(all, ret, _str.len); \
42  BIO_free_all(base64); \
43  xroad_str_len(ret, len); \
44 })
45 
46 #ifdef __cplusplus
47 }
48 #endif