0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: Base64 encoding (using crypto/base64 lib)

DFRobot Mar 10 2019 2520

In this ESP32 tutorial we will check how to do the base64 encoding of a string, using the ESP32 and the Arduino core. The tests of this tutorial were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

ESP32 Arduino: Base64 encoding (using crypto/base64 lib)


Introduction

In this tutorial we will check how to do the base64 encoding of a string, using the ESP32 and the Arduino core.

Note that we have already covered the base64 encoding procedure using another library on this previous post. Nonetheless, the mentioned library doesn’t offer a way of also decoding the base64 string back to plain text, which may be needed for certain applications.

On other tutorial, we have covered the decoding procedure using a library called crypto/base64.h. This is a more complete library because it offers both the decoding and encoding methods, although it has a lower level API.

So, for completion and in order to be able to use only the crypto/base64.h  library in a project that needs both the encoding and decoding steps, we will cover here how to encode a plain text string using it.

The tests of this tutorial were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start our code by including the crypto/base64.h. The include needs to be enclosed in a extern “C” block so we don’t have compilation problems.

extern "C" {
#include "crypto/base64.h"
}

Moving on to the setup function, we will start by opening a serial connection, so we can output the base64 encoded string.

Serial.begin(115200);

Then, we will declare the plain text string that we will encode. It will be a very simple “Hello World” message.

char * toEncode = "Hello World";

Additionally, we will need to declare a variable of type size_t. We will later pass the address of this variable to the base64 encoding function, which will use it to return the size of the encoded content.

size_t outputLength;

Then, to perform the actual encoding, we need to call the base64_encode function. As first input, this function receives the string to encode, which we have previously declared. Note however that the function expects a const unsigned char pointer, which means that we need to do a cast on our string.

As second argument, the encoding function receives the length of the plain text string. This length can be obtained with the strlen function.

Finally, as third argument, we need to pass the address of our size_t variable, which will be used by the base64_encode function to set the length of the encoded string.

This function returns as output the base64 encoded string, as an unsigned char pointer.

unsigned char * encoded = base64_encode((const unsigned char *)toEncode, strlen(toEncode), &outputLength); 

After this, we will print both the length of the encoded message and then its content. Note that we are going to make use of the printf method of the Serial object and we are going to use the %.*s format specifier to print the message.

Serial.print("Length of encoded message: ");
Serial.println(outputLength);
 
Serial.printf("%.*s", outputLength, encoded)

To finalize, we need to free the memory allocated for the buffer with the encoded message. As indicated in the library source file, it’s responsibility of the caller of the base64_encode function to free the returned buffer.

free(encoded);

The final code can be seen below.

extern "C" {
#include "crypto/base64.h"
}
 
void setup() {
  Serial.begin(115200);
 
  char * toEncode = "Hello World";
  size_t outputLength;
 
  unsigned char * encoded = base64_encode((const unsigned char *)toEncode, strlen(toEncode), &outputLength);
 
  Serial.print("Length of encoded message: ");
  Serial.println(outputLength);
 
  Serial.printf("%.*s", outputLength, encoded);
  free(encoded);
}
 
void loop() {}

Testing the code

To test the Arduino code, compile it and upload it to your device. Once the procedure finishes, open the Arduino IDE serial monitor. You should get an output similar to figure 1, which shows the encoded message and its length.


You can copy the encoded message and paste it on this online tool to confirm it decodes back to the original message correctly. Figure 2 illustrates this comparison.