Failure to understand the Nodejs base64 encode process could hinder you from maximizing the runtime environment's potential in data transfer.
This tutorial explains Nodejs' significance in data encoding. It walks you from manual to the terminal to (simple and complex) Nodejs file encoding.
The Buffer class plays a significant role in Nodejs base64 encode. To encode data, specify it in the Buffer instance and string base64 encoding format, as follows.
new Buffer.from(<data to encode>).toString('base64')
As you will see in this tutorial, you can also decode the data while reading it. We will use Visual Studio Code but you can replace it with your preferred code editor.
Let's get started.
Why base64 encoding?
You can skip this section if you only want to see practical examples of Nodejs base64 encode.
The relevance of data encoding emerged during the teleprinter era. You could type a key in the simple machine, which changed the characters to numbers. The encoded message could be printed and faxed over long distances.
Then came the need for standardizing telecommunication. ASCII developed a 7-bit methodology to map the English letters and special characters to numbers.
The resulting decimal numbers could be converted, through the ASCII table, to binary (1s and 0s) for inter-computer communication.
The main drawback of ASCII was its inability to encode non-English characters. The solution to that challenge was the introduction of Unicode, which accommodates more than 100000 characters.
Another challenge during the internet era was transferring the 7-bit data (0 to 127 characters) through an 8-bit network. That is where base64 encoding came in.
Base64 encoding and how it works
Unicode uses various decoding and encoding formats. The notable ones are UTF-8
, UTF-16
, and base64
.
Converting a Buffer into a string using either UTF-8
, UTF-16
, or Latin1
is called decoding. On the other hand, converting a string into a Buffer is called encoding.
In the same vein, you can do binary to text encoding using base64
, hex
, or base64url
. The key takeaway is that base64 encodes binary data, whereas UTF-8
and UTF-16
encode Unicode text.
Another fact worth noting is that the ASCII Table uses 128 characters to represent values arithmetically. On the other hand, Base64 Indexing Table has 64 symbols to network-wise represent values.
Base64 simplifies encoding various data formats like text, images, videos, HTML, and JSON files. Say we want to encode "Hey." We can do it manually, using the terminal commands, or Nodejs base64 encode the data.
Manual encoding
The five main steps to encode data are:
Step-1: Locate the integer mapping of each character from the ASCII Table.
That is, H = 72, e = 101, y = 121
Step-2: Calculate the 8-bit version of the characters.
Character | ASCII | 2 power 7 | 2 power 6 | 2 power 5 | 2 power 4 | 2 power 3 | 2 power 2 | 2 power 1 | 2 power 0 |
---|---|---|---|---|---|---|---|---|---|
H | 72 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
e | 101 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 |
y | 121 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 1 |
Step-3: Regroup the step-2 results into 6-bit chunks.
group 0 | group 1 | group 2 | group 3 |
---|---|---|---|
0 1 0 0 1 0 | 0 0 0 1 1 0 | 0 1 0 1 0 1 | 1 1 1 0 0 1 |
Fill the empty spaces with 0s if the last group has less than 6 bits. Note: The name base64 results from the 6-bit chunk grouping. That is 2 power 6 = 64.
Step-4: Convert each of the 6-bit chunks into decimal equivalents, respectively.
Binary | Decimal |
---|---|
0 1 0 0 1 0 | 18 |
0 0 0 1 1 0 | 6 |
0 1 0 1 0 1 | 21 |
1 1 1 0 0 1 | 57 |
Step-5: Find the equivalent mapping of each of the step-4 decimal equivalents in the Base64 Indexing Table.
Therefore, the encoded form of Hey is SGV5.
The weakness of manual encoding is that it is tedious and time-consuming. That is why you should check out the terminal encoding.
Terminal encoding
You can pipe a string to the base64 command on the terminal.
echo <data to encode> | base64
For example, we can encode "Great" as follows.
echo "Great" | base64
The result is R3JlYXQK.
Conversely, we can decode the encoded data using the --decode
option.
echo <encoded data> | base64 --decode
For example, we can decode "R3JlYXQK" as follows.
echo R3JlYXQK | base64 --decode
The result is Great.
Although you can encode data on the terminal, using a runtime environment or a programming language and a code editor simplifies handling complex data. Let us explore that using Nodejs base64 encode.
Nodejs base64 encode with examples
Understanding Nodejs base64 encode starts by knowing how the runtime environment handles data transfer. One of the selling points of Nodejs is its fast data transfer.
The prompt data transfer is possible through single threading. Instead of waiting for a chunk of data to arrive before consuming it, Nodejs sends smaller portions of the data in streams. The ready-for-consumption data is called Buffer and gets temporarily stored in computer memory.
Although Nodejs handles Buffer management internally, we can create one for demo purposes.
// Create a Buffer, allocating 5-byte size memory
const buf = Buffer.alloc(5)
// Store HI! in the created memory
buf.write("HI!")
// Show the ASCII mapping of each character
console.log(`Mapping each character (${buf.toString()}) => [${buf[0]}, ${buf[1]}, ${buf[2]}]`)
As you can see, Buffer is an array of bits. We can use it for Nodejs base64 encode as follows.
Example-1: Nodejs base64 encode a string
A string is a collection of characters. So, encoding a string implies mapping each character to Base64 Indexing Table, as we did manually in section two of this tutorial.
Assume we want to encode, "Ahmed loves programming." We can achieve that as follows.
// Encode the input
const message = new Buffer.from('Ahmed loves programming').toString('base64')
// print the result
console.log(`The encoded message is ${message}`)
Similarly, we can decode the message as follows:
const decodedMessage = new Buffer.from('QWhtZWQgbG92ZXMgcHJvZ3JhbW1pbmc=', 'base64').toString('ascii')
console.log(`Decoded message is ${decodedMessage}`)
Here, I am calling an instance of the global Buffer class with the toString()
method to encode data, Ahmed loves programming, to base64 and later decode it to ASCII.
A similar encoding format applies to other file types like HTML, JSON, or image.
Example-2: Nodejs base64 encode JSON
Create files, users.json
and buffer.js
. Assume we want to read and encode the users.json
file. We can do that as follows.
Add the following contents in users.json
[
{
"id": 1,
"name": "Lenny Grand",
"username": "Brethen",
"email": "Sincere@april.biz",
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org"
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"phone": "010-692-6593 x09125",
"website": "anastasia.net"
}
]
Open the buffer.js
file, read and encode the users.json
file as follows.
// import the fs module
const fs = require('fs');
// read a json file
const inJSON = fs.readFileSync('users.json')
// get base64 from the json file
const encodedJSON = new Buffer.from(inJSON).toString('base64')
// print the result
console.log(`\n${encodedJSON}`)
The result is
Ww0KICAgIHsNCiAgICAgICAgImlkIjogMSwNCiAgICAgICAgIm5hbWUiOiAiTGVubnkgR3JhbmQiLA0KICAgICAgICAidXNlcm5hbWUiOiAiQnJldGhlbiIsDQogICAgICAgICJlbWFpbCI6ICJTaW5jZXJlQGFwcmlsLmJpeiIsDQogICAgICAgICJwaG9uZSI6ICIxLTc3MC03MzYtODAzMSB4NTY0NDIiLA0KICAgICAgICAid2Vic2l0ZSI6ICJoaWxkZWdhcmQub3JnIg0KICAgIH0sDQogICAgew0KICAgICAgICAiaWQiOiAyLA0KICAgICAgICAibmFtZSI6ICJFcnZpbiBIb3dlbGwiLA0KICAgICAgICAidXNlcm5hbWUiOiAiQW50b25ldHRlIiwNCiAgICAgICAgImVtYWlsIjogIlNoYW5uYUBtZWxpc3NhLnR2IiwNCiAgICAgICAgInBob25lIjogIjAxMC02OTItNjU5MyB4MDkxMjUiLA0KICAgICAgICAid2Vic2l0ZSI6ICJhbmFzdGFzaWEubmV0Ig0KICAgIH0NCl0=
Assume, we are only want to encode Ervin Howell's phone number (from the JSON file), we can that as follows.
// import the fs module
const fs = require('fs')
// read a json file
const oldJSON = fs.readFileSync('users.json')
// convert the json file into Buffer array and encode the phone number
const usersArray = JSON.parse(oldJSON)
const encodedPhone = new Buffer.from(usersArray[1].phone).toString('base64')
usersArray[1].phone = encodedPhone
// convert the object to json
const newJSON = JSON.stringify(usersArray)
// print the result
// 1. as literals
console.log(usersArray)
// 2. in json
console.log(newJSON)
The result is
[
{
id: 1,
name: 'Lenny Grand',
username: 'Brethen',
email: 'Sincere@april.biz',
phone: '1-770-736-8031 x56442',
website: 'hildegard.org'
},
{
id: 2,
name: 'Ervin Howell',
username: 'Antonette',
email: 'Shanna@melissa.tv',
phone: 'MDEwLTY5Mi02NTkzIHgwOTEyNQ==',
website: 'anastasia.net'
}
]
[{"id":1,"name":"Lenny Grand","username":"Brethen","email":"Sincere@april.biz","phone":"1-770-736-8031 x56442","website":"hildegard.org"},{"id":2,"name":"Ervin Howell","username":"Antonette","email":"Shanna@melissa.tv","phone":"MDEwLTY5Mi02NTkzIHgwOTEyNQ==","website":"anastasia.net"}]
Example-3: Nodejs base64 encode an Image
Say we want to encode the linked image.
We could download it, create a server, upload the image, encode and store it in a directory. Nodejs middleware like formidable or multer could help in the upload process.
Instead, we will download the image as image.png
. Next, Nodejs base64 encode the image, create a directory, decode and copy the image there.
// import the fs module
const fs = require('fs')
// directory to check if exists
const dir = './uploads';
// check if the uploads directory does NOT exists and create it.
if (!fs.existsSync(dir)) {
fs.mkdir('uploads', err => {
if(err) throw err
console.log('Directory created')
})
}
// read image as binary
const binaryImage = fs.readFileSync('image.png')
// get base64 from binaryImage
const base64Image = new Buffer.from(binaryImage).toString('base64')
// create a folder and save the base64Image there
fs.writeFile('uploads/image.png', base64Image, {encoding: 'base64'}, err => {
if (err) throw err
console.log(`Kudos, you have succeeded!`)
})
Open the new directory.
And voila, we could base64 encode/decode an image and copy it in the new directory!
Conclusion
It help would have a deep knowledge of Nodejs base64 encode. Then, you can apply it in text ciphering, JSON, and image encoding, as explained in this tutorial.