Base64

TL;DR

Sometimes the internet requires us to deal with Base64-encoded stuff.

From Base64:

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

And also…

Base64 is particularly prevalent on the World Wide Web.

So… what to do about it? Usually, Linux distributions have a utility for it: base64.

Using base64 is straightforward, as it can act as a filter:

$ printf 'whatevah!' | base64
d2hhdGV2YWgh

Note that it encodes everything you give it as input, even a newline. This can introduce infuriating bugs when encoding username and passwords for Basic Authentication, so it’s better to use printf without a newline, like in the example above or the following:

$ printf '%s:%s' "$username" "$password" | base64
...

Decoding is easy with the -d option:

$ printf 'd2hhdGV2YWgh' | base64 -d ; echo
whatevah!

The final ; echo makes sure that the prompt is printed on a newline… because we did not include one in the encoded string, as described above.

Happy Base64 encoding!


Comments? Octodon, , GitHub, Reddit, or drop me a line!