macOS Command - "base64"

This section provides a tutorial example on how to use 'base64' command on a macOS system to perform Base64 encoding and decoding.

If you are using a macOS system, you can use the built-in command "base64" to perform Base64 encoding and decoding.

Here is the manual page for "base64":

herong$ man base64

NAME
base64 -- Encode and decode using Base64 representation

SYNOPSIS
     base64 [-h | -D] [-b count] [-i input_file] [-o output_file]

DESCRIPTION
base64 encodes and decodes Base64 data, as specified in RFC 4648. With no
options, base64 reads raw data from stdin and writes encoded data as a
continuous block to stdout.

OPTIONS
The following options are available:
-b count
--break=count      Insert line breaks every count characters. Default is 0,
                   which generates an unbroken stream.
-D
--decode           Decode incoming Base64 stream into binary data.
-h
--help             Print usage summary and exit.
-i input_file
--input=input_file Read input from input_file.  Default is stdin;
                   passing - also represents stdin.
-o output_file
--output=output_file
                   Write output to output_file.  Default is stdout;
                   passing - also represents stdout.

Test 1 - Let's encode a short text string of "ABC" from a file, which can be created using the "cat > input.txt" command. We need to press Ctrl-D and Ctrl-C to end the input stream from the keyboard.

herong$ cat > input.txt
ABC^D^C

herong$ base64 -i input.txt
QUJD

Test 2 - Let's decode a short encoded string of "QUJD" back.

herong$ base64 -i input.txt -o output.txt

herong$ more output.txt
QUJD

herong$ base64 -D -i output.txt -o decoded.txt

herong$ more decoded.txt
ABC

Test 3 - Let's encode the "lazy dog" message:

herong$ more lazy_dog.txt
The quick brown fox jumps over the lazy dog.

herong$ base64 -i lazy_dog.txt 
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=

Test 4 - Let's encode a binary file:

herong$ curl http://herongyang.com/_logo.png > icon.png  

herong$ base64 -i icon.png 
iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

Test 4 - Let's wrap the encoded into 76-character lines to meet the MIME (Multipurpose Internet Mail Extensions) specification.

herong$ base64 -b 76 -i icon.png

iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAEYSURBVEhL7ZbLDcIwDIYdBGfYADahm1E24QRr
9AxLNBvAuUjgJE7ruI5S8RAXPslqaltxfsdSax4IfJEZPQeMSa2EyLdmTwHCKUhwLmYt1BTIIPIl
eovkyZUUj8zrrgDzJb0EQovuN64SbFt7d4/cCLE2bYWFCsxi2e9xutxCwOvormOV3OEMcxJkXLiO
55A/vuSIy+MsVrTA0+5TRfbQ0ErBl9EUIO2hGpxobUUXznzeCO6KCkI0U8DDA5oxuLvcooibjAy2
Zq3BQdEoF8CxsxWtBevdllZ51AJx1KJtGlQtwU4keTiiGmUFGdymU3i5wFTUAuk8BJOMcrpXL/lN
/gWKfK6A+A5EpheQYzORr7foB38VHwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

Table of Contents

 About This Book

 Base64 Encoding

Base64 Encoding and Decoding Tools

 Base64.Guru - Base64 Online Tool

 Windows Command - "certutil -encode/-decode"

 Linux Command - "base64"

macOS Command - "base64"

 Java Built-In Implementation of Base64

 Java Built-In Implementation of MIME Base64

 Python Built-In Implementation of Base64

 Python Built-In Implementation of MIME Base64

 PHP Built-In Implementation of Base64

 PHP Built-In Implementation of MIME Base64

 Perl Built-In Implementation of Base64

 Perl Built-In Implementation of MIME Base64

 Base64URL - URL Safe Base64 Encoding

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 UUEncode Encoding

 References

 Full Version in PDF/EPUB