Essential Base64 Commands For Developers

This post provides a quick reference to the base64 commands that I use on macOS. The base64 command encodes and decodes Base64 data, as specified in RFC 4648.
This post provides a quick reference to the base64 commands that I use on macOS. This is not a complete set of base64 commands with detailed explanations on what each command does. If you want to read detailed documentation on the different options and arguments of the base64 command, please refer to the man pages of the base64 command.
The base64 command encodes and decodes Base64 data, as specified in RFC 4648. With no options, the base64 commands reads raw data from stdin and writes encoded data as a continuous block to stdout.
What is Base64?
The Base 64 encoding is designed to represent arbitrary sequences of octets in a form that allows the use of both upper- and lowercase letters but that need not be human readable. A 65-character subset of US-ASCII is used, enabling 6 bits to be represented per printable character. (The extra 65th character, “=”, is used to signify a special processing function.) ~ RFC4648
How does Base64 Work?
The encoding process represents 24-bit groups of input bits as output strings of 4 encoded characters. Proceeding from left to right, a 24-bit input group is formed by concatenating 3 8-bit input groups. These 24 bits are then treated as 4 concatenated 6-bit groups, each of which is translated into a single character in the base 64 alphabet. Each 6-bit group is used as an index into an array of 64 printable characters. The character referenced by the index is placed in the output string. ~ RFC4648
Index Table
The Base64 index table looks as follows:
Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 000000 | A | 16 | 010000 | Q | 32 | 100000 | g | 48 | 110000 | w | |||
1 | 000001 | B | 17 | 010001 | R | 33 | 100001 | h | 49 | 110001 | x | |||
2 | 000010 | C | 18 | 010010 | S | 34 | 100010 | i | 50 | 110010 | y | |||
3 | 000011 | D | 19 | 010011 | T | 35 | 100011 | j | 51 | 110011 | z | |||
4 | 000100 | E | 20 | 010100 | U | 36 | 100100 | k | 52 | 110100 | 0 | |||
5 | 000101 | F | 21 | 010101 | V | 37 | 100101 | l | 53 | 110101 | 1 | |||
6 | 000110 | G | 22 | 010110 | W | 38 | 100110 | m | 54 | 110110 | 2 | |||
7 | 000111 | H | 23 | 010111 | X | 39 | 100111 | n | 55 | 110111 | 3 | |||
8 | 001000 | I | 24 | 011000 | Y | 40 | 101000 | o | 56 | 111000 | 4 | |||
9 | 001001 | J | 25 | 011001 | Z | 41 | 101001 | p | 57 | 111001 | 5 | |||
10 | 001010 | K | 26 | 011010 | a | 42 | 101010 | q | 58 | 111010 | 6 | |||
11 | 001011 | L | 27 | 011011 | b | 43 | 101011 | r | 59 | 111011 | 7 | |||
12 | 001100 | M | 28 | 011100 | c | 44 | 101100 | s | 60 | 111100 | 8 | |||
13 | 001101 | N | 29 | 011101 | d | 45 | 101101 | t | 61 | 111101 | 9 | |||
14 | 001110 | O | 30 | 011110 | e | 46 | 101110 | u | 62 | 111110 | + | |||
15 | 001111 | P | 31 | 011111 | f | 47 | 101111 | v | 63 | 111111 | / | |||
Padding | = |
Command Examples
1. Help and Information Commands
To find help or learn more about the different options and arguements used as part of the base64 command, you can type the following in the terminal console.
Command:
# Display quick help information about the base64 command
$ base64 --help
2. Encode Raw Message (STDIN)
The base64 commands reads raw data from stdin and writes encoded data as a continuous block to stdout.
Command:
# Read raw data from stdin and write encoded data to stdout
$ echo 'Hello World' | base64
3. Decode Encoded Message (STDIN)
The base64 commands reads encoded data from stdin and writes decoded data as a continuous block to stdout.
Command:
# Read raw data from stdin and write encoded data to stdout
$ echo 'SGVsbG8gV29ybGQK' | base64 --decode
4. Encode Raw Message (Files)
The base64 commands reads raw data from an input file and writes encoded data as a continuous block to an output file.
Command:
# Read raw data from input file and write encoded data to output file.
$ base64 -i raw_message.txt -o encoded_message.txt
5. Decode Encoded Message (Files)
The base64 commands reads encoded data from an input file and writes decoded data as a continuous block to an output file.
Command:
# Read encoded data from input file and write decoded data to output file.
$ base64 -d -i encoded_message.txt -o decoded_message.txt
Summary
The base64 commands listed in this article is by no means a complete set of base64 command and only serve as a quick reference to be used by developers. To see the full documentation of the base64 command, please read the man pages of the base64 command.
Follow me on any of the different social media platforms and feel free to leave comments.