How to do Base64 Encoding and Decoding in Node.js

Base64 encoding is a technique used to convert binary data to ASCII characters. Mastering this technique in Node.js can prove helpful to do many tasks such as transmitting data over a network, encoding binary files, and storing data in text formats. In this article, we’ll look at how we can implement a Base64 converter in Node.js along with some examples for better understanding.

How Base64 Encoding Works

The main concept behind Base64 encoding is converting sequences of binary data which is of 8-bit length each and representing them in 4 segments of 6 bits, making sequences of 24 bits. Let us understand the steps to do Base64 encoding.

Steps of Base64 encoding:

  1. First, each character in the input text is converted to its corresponding ASCII value.
  2. The ASCII values are converted into their 8-bit binary equivalent.
  3. The 8-bit binary data is broken into 4 segments of 6-bits each creating a sequence of 24 bits.
  4. Then using the Base64 table, we convert the 6-bit binary values into their corresponding index values.
  5. Finally, the index values are converted to their corresponding Base64 values by referring to the Base64 table.

Base64 Encoding in Node.js

To implement Base64 encoding in Node.js we need to install the Buffer package containing Buffer classes which helps work with binary data conversions in Node. We can then import it into our project using require().

Then we use a constant to represent the input string (inputString in this case). We then use Buffer.from() to create a new Buffer object from the input string which is stored in the buffer constant. By calling Buffer.from() on the input string we convert the input string into its 8-bit binary form.

Finally, we encode the buffer into a Base64 String using the toString() method and store it in a constant called base64String.

Examples of Base64 Encoding

Let’s see some examples of Base64 encoding.

Example 1:

const { Buffer } = require('buffer');

const inputSring = 'Hey';
const buffer = Buffer.from(inputSring);
const base64String = buffer.toString('base64');

console.log(base64String);

Output:

SGV5

Example 2:

Let us take another example, with a string of longer length. This time we’ll see how the same code above can be written in just 2 lines!

const { Buffer }  = require('buffer');
console.log(Buffer.from("I am Groot").toString('base64'))

Output:

SSBhbSBHcm9vdA==

Base64 Decoding in Node.js

To decode a Base64 encoded string to its original input string, we can just reverse the above process while using the same Buffer package.

We first import Buffer into our file using require(). Then we input the Base64 string, which we’ll store in a constant base64String. Then we use the Buffer.from() method on base64String this time, to initiate a new Buffer object and convert it into its binary equivalent. The binary equivalent is stored in a constant called buffer.

We finally obtain the original string by calling the toString() method on buffer, which converts the binary data into a string. The original string is stored in a constant originalString.

Examples of Base64 Decoding

Let’s now see some examples of Base64 decoding.

Example:

Taking the output from the previous example 1 as the Base64 input string, we will obtain the input string from before as the original string.

const { Buffer } = require('buffer');

const base64String = 'SGV5';
const buffer = Buffer.from(base64String, 'base64');
const originalString = buffer.toString('utf-8');

console.log(originalString);

Output:

Hey

Example 2:

Now, let’s convert the Base64 string get from example 2 to its original string.

const { Buffer } = require('buffer');

const encodedString = Buffer.from("I am Groot").toString('base64');
console.log(encodedString);

console.log(Buffer.from(encodedString, 'base64').toString('utf-8'));

Output:

SSBhbSBHcm9vdA==
I am Groot

Conclusion

Base64 encoding and decoding are simple yet effective techniques used to convert binary data into a Base64 string or the other way around. We have seen how these techniques can be easily implemented with the help of the Buffer package provided by Node.js. By following a few simple steps encoding and decoding Base64 strings can easily be achieved. With the help of examples, we will now be able to efficiently implement these techniques.

Reference

https://stackoverflow.com/questions/6182315/how-can-i-do-base64-encoding-in-node-js

Nandana Pradosh
Nandana Pradosh
Articles: 27