Last active
March 11, 2022 19:45
-
-
Save VNovytskyi/9f9f2449733d1294d0f2a7b978b1c1b9 to your computer and use it in GitHub Desktop.
Arduino ESP32 Base64 encoding with MBedTLS
This file contains 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 characters
#include "mbedtls/base64.h" | |
#include "mbedtls/error.h" | |
size_t get_encoded_size(size_t plain_data_size) { | |
return (4 * ceil((double)plain_data_size / 3)) + 1; | |
} | |
void setup() { | |
const char *test_data_p = "Hello world"; | |
size_t test_data_length = strlen(test_data_p); | |
size_t encoded_data_size = get_encoded_size(test_data_length); | |
uint8_t *encode_data_buff = (uint8_t*)malloc(encoded_data_size); | |
if (encode_data_buff == NULL) { | |
printf("Failed to allocate %zu bytes...\n", encoded_data_size); | |
return; | |
} | |
size_t encode_data_length = 0; | |
int res = mbedtls_base64_encode( | |
encode_data_buff, | |
encoded_data_size, | |
&encode_data_length, | |
(const uint8_t*)test_data_p, | |
test_data_length | |
); | |
if (res != 0) { | |
char err_buff_str[128]; | |
mbedtls_strerror(res, err_buff_str, sizeof(err_buff_str)); | |
printf("Failed encode data, error: %s\n", err_buff_str); | |
free(encode_data_buff); | |
return; | |
} | |
printf("Data [%zu]: %s\n", test_data_length, test_data_p); | |
printf("Encoded data [%zu]: %s\n", strlen((char*)encode_data_buff), (char*)encode_data_buff); | |
free(encode_data_buff); | |
} | |
void loop() { | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment