DEV Community

Cover image for How to Base64 Encode a String in Terminal
Shannon Crabill
Shannon Crabill

Posted on • Originally published at shannoncrabill.com on

How to Base64 Encode a String in Terminal

While browsing some API documentation, I saw references to Base64 for passing credentials to the API. I had seen Base64 referenced a few times, but had no idea how to convert text into Base64.

So, I did some digging.

This post will outline how to encode (and decode) text into Base64 using the MacOS Terminal.

What is Base64?

I should share a little bit about what Base64 is. The MDN documentation explains the overarching concept of Base64 as.

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.

You can think of Base64 is another way to represent binary or text data.

The conversion process is somewhat detailed, and I encourage you to check out the resources at the bottom of this article if you are curious about the benefits and technical details of Base64.

Converting Text into Base64

While reading those API docs, I had no idea how to create a Base64 string.

There are converters a few Google searches away. But, did you know there’s a Base64 command built into the MacOS?

To turn a string into Base64, open a new Terminal window, and use this format.

base64 <<< string
Enter fullscreen mode Exit fullscreen mode

That would be base64 followed by a space, three less-than signs (<), another space, then whatever string you want to encode in Base64.

Here’s an example:

If I wanted to convert the string I love cheeseburgers into Base64, I would enter this:

base64 <<< "I love cheeseburgers"
Enter fullscreen mode Exit fullscreen mode

Which would print out SSBsb3ZlIGNoZWVzZWJ1cmdlcnMK as a result.

Note: You only need quote marks around the string you would like to convert if it has space it in.

The following does not need quote marks, for example:

base64 <<< ilovecheeseburgers
Enter fullscreen mode Exit fullscreen mode

If you're curious, base64 <<< ilovecheeseburgers returns aWxvdmVjaGVlc2VidXJnZXJzCg== which is very different from what base64 <<< "I love cheeseburgers" returns.

Decoding Base64

With a similar command, you can decode Base64 back into human, readable text.

Let’s say a friend of ours sent us the following code:

SSBsb3ZlIHlvdSBtb3JlCg==

To decode Base64, we add a -D flag before the three arrows and after base64.

Like this:

base64 -D <<< SSBsb3ZlIHlvdSBtb3JlCg==
Enter fullscreen mode Exit fullscreen mode

Which returns I love you more. How sweet!

Base64 is Not Unique

While a Base64 string like SSBsb3ZlIGNoZWVzZWJ1cmdlcnMk looks random, it is not.

Each time I run base64 <<< "I love cheeseburgers" it’ll return the same result. If you run the same code, you’ll get the same result. And since a Base64 string can be decoded, it’s not appropriate for hashing passwords, storing API keys, etc.

Resources

The post Base64 Encoding appeared first on Shannon Crabill — Front End Software Engineer.

Top comments (5)

Collapse
 
danroe profile image
Daniel Roe • Edited

Just a note, it seems that using the command with <<< doesn't take into account that the string ends there and adds extra characters at the end such as \n (next line character).

To avoid this causing the base64 to be incorrect, I would use the following commands instead:

echo -n 'some string here' | base64 to encode
echo -n 'c29tZSBzdHJpbmcgaGVyZQ==' | base64 -d to decode

"SSBsb3ZlIGNoZWVzZWJ1cmdlcnMK" in base64 is:

I love cheeseburgers

Enter fullscreen mode Exit fullscreen mode

The final empty line is not a mistake, it really did encode an empty line.

Using the method specified above, the base64 should be SSBsb3ZlIGNoZWVzZWJ1cmdlcnM= (note the last character has changed since it's no longer encoding the new line character)

Collapse
 
okayashik profile image
Rifat Khan

Dear,

it's a very simple command by the terminal.

Image description

You can also use the tools to encode and decode Base64
techinblood.com/base64-encoder-and...

Collapse
 
marcosguilhermef profile image
marcosguilhermef

I use toolsfordevs.com.br/base64/ec to convert text to base64 and revert. This site is very nice!

Collapse
 
arnabsen1729 profile image
Arnab Sen

In simple terms base64 follows symmetric encryption.

Collapse
 
danroe profile image
Daniel Roe

I wouldn't suggest calling it encryption, it's just an encoding.

Encryption / Decryption can be done with AES or other symmetric / asymmetric key algorithms used for keeping data safe.

Encoding / Decoding can be done with base64, binary, hexadecimal, ROT-13 and others that also change the representation method of the data (these should not be used for encryption, they're mostly used to represent data in a way that fits the purpose and sometimes to avoid certain characters that would cause issues when translated for data transfer / passing between different systems / sub-systems).

Hashing can be done with SHA or other mathematical functions that can only convert in a single direction (hopefully) and is used for anything where only the originator should know the original source (such as a password) or even for the purposes of verifying that a file produces the same hash as it should (making sure the file has not been tampered with).