[Awesome Software CLI] Base64: Convert data as plain text in order to prevent data corruption

Base64

Base64 is the most popular binary-to-text algorithm used to convert data as plain text in order to prevent data corruption during transmission between different storage mediums. In addition, it is often used to embed binary data into text documents such as HTML, CSS, JavaScript, or XML. Given the high popularity of the algorithm, Base64 is well documented, is supported by various programming languages, has spawned several modifications, and, of course, has its own specification.

Base64 is most commonly used to encode binary data (for example, images, or sound files) for embedding into HTML, CSS, EML, and other text documents. In addition, Base64 is used to encode data that may be unsupported or damaged during transfer, storage, or output. Here are some of the applications of the algorithm:

  • Attach files when sending emails

  • Embed images in HTML or CSS via data URI

  • Preserve raw bytes of cryptographic functions

  • Output binary data as XML or JSON in API responses

  • Save binary files to database when BLOB is unavailable

  • Hide secrets from prying eyes (really a very bad idea)

Command Line Tool (CLI)

Installation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# macOS
$ brew install base64

# Debian, Ubuntu, Kali Linux, Raspbian
$ apt-get install coreutils

# Alpine
$ apk add coreutils

# Arch Linux
$ pacman -S coreutils

# CentOS
$ yum install coreutils

# Fedora
$ dnf install coreutils

# Docker
$ docker run --rm cmd.cat/base64 base64 --help

Commands

base64 help:

1
2
3
4
5
6
7
$ base64 -h
Usage: base64 [-hvDd] [-b num] [-i in_file] [-o out_file]
-h, --help display this message
-Dd, --decode decodes input
-b, --break break encoded string into num character lines
-i, --input input file (default: "-" for stdin)
-o, --output output file (default: "-" for stdout)

Encode and Decode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Encode
$ echo cloudolife.com | base64
Y2xvdWRvbGlmZS5jb20K

# Decode
$ echo Y2xvdWRvbGlmZS5jb20K | base64 -d

# Encode and decode
$ echo cloudolife.com | base64 | base64 -d
cloudolife.com

# Encode from file
$ echo cloudolife.com > cloudolife.txt && base64 -i cloudolife.txt
Y2xvdWRvbGlmZS5jb20K

# Encode from file, and decode from file
$ echo cloudolife.com > cloudolife.txt && base64 -i cloudolife.txt > cloudolife.base64 && base64 -d -i cloudolife.base64
cloudolife.com

Online Tool

Base64 - https://base64.guru/

Security

Base64 is not an encryption algorithm and in no case should it be used to “hash” passwords or “encrypt” sensitive data, because it is a reversible algorithm and the encoded data can be easily decoded. Base64 may only be used to encode raw result of a cryptographic function.

Roughly speaking, in terms of information security, Base64 is just a foreign language that some people do not understand. Nevertheless, even they can understand the meaning of the encoded message simply by using an online translator, which instantly returns the original message.

FAQs

base64: invalid input

A likely reason for 76 being the default is that Base64 encoding was to provide a way to include binary files in e-mails and Usenet postings which was intended for humans using monitors with 80 characters width. Having a 76-character width as default made that usecase easier.

-w, --wrap=COLS will wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping.

For macOS, use -b, --break to break encoded string into num character lines.

1
2
3
4
$ echo cloudolife.com | base64 -w 0

# macOS
# $ echo cloudolife.com | base64 -b

References

[1] Base64 - Wikipedia - https://en.wikipedia.org/wiki/Base64

[2] Base64 - https://base64.guru/

[3] base64 — Homebrew Formulae - https://formulae.brew.sh/formula/base64

[4] command-not-found.com – base64 - https://command-not-found.com/base64

[5] Coreutils - GNU core utilities - https://gnu.org/software/coreutils

[6] Base64 Converter | Base64 - https://base64.guru/converter