15

Base64 Encoding

 3 years ago
source link: https://shannoncrabill.com/blog/base64-encoding-mac-terminal/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 is.

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 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

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"

Which would print out SSBsb3ZlIGNoZWVzZWJ1cmdlcnMK as a result.

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

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==

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

Related


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK