How to encode base64 via command line?

Is there a terminal command in Mac OS X which will base64 encode a file or stdin?

2

17 Answers

openssl can do this for you, and it's all installed with OS X by default; no need to install darwinports.

$ openssl base64 -in <infile> -out <outfile>

Without the -in option reads from stdin

9

Openssl can be used more succinctly:

echo -n 'input' | openssl base64

[ echo -n -> must be used, or encoding will be done including new line character ]

or

openssl base64 <ENTER> [type input] <CTRL+D>
6

Try using:

base64 -i <in-file> -o <outfile>

It should be available by default on OS X.

3

On macOS I always use:

echo -n "STRING" | base64

-n is to avoid a new line character on the end of the line.

2

base64 command is available by default on my OS X 10.9.4.

You can use base64 <<< string and base64 -D <<< string to encode and decode a string in the terminal, or base64 -in file and base64 -D -in file to encode and decode a file.

4

Since Python is provided with OS X by default, you can use it as below:

$ echo FOO | python -m base64
Rk9PCg==
$ echo Rk9PCg== | python -m base64 -d
FOO

Or install coreutils via Brew (brew install coreutils) which will provide base64 command:

$ echo FOO | base64
Rk9PCg==
$ echo Rk9PCg== | base64 -d
FOO

You can also pipe it right to the clipboard (at least on mac):

openssl base64 -in [filename] | pbcopy

2

Python

Python comes preinstalled on all macs nowadays.

In Terminal run python (or ipython).

Encode a file:

 base64data = open('myfile.jpg','rb').read().encode('base64') open('myfile.txt','w').write(base64data)

Decode a file:

data = open('myfile.txt').read().decode('base64')
open('myfile.jpg','wb').write(data)

Of course, both operations can be converted to a oneliner but this way it is more readable.

OpenSSL

## encode to base64 (on OSX use `-output`)
openssl base64 -in myfile.jpg -output myfile.jpg.b64
## encode to base64 (on Linux use `-out`)
openssl base64 -in myfile.jpg -out myfile.jpg.b64
## decode from base64 (on OSX `-output` should be used)
openssl base64 -d -in myfile.jpg.b64 -output myfile.jpg
## decode from base64 (on Linux `-out` should be used)
openssl base64 -d -in myfile.jpg.b64 -out myfile.jpg

Omitting the -out/-output... filename will print to stdout.

base64

Another ootb utility present both in OSX and Ubuntu:

## encode to base64
base64 < myfile.jpg > myfile.jpg.b64
## decode from base64 (OSX) (note the uppercase 'D')
base64 -D < myfile.jpg.b64 > myfile.jpg
## decode from base64 (Linux) (note the lowercase 'd')
base64 -d < myfile.jpg.b64 > myfile.jpg

In terms of speed, I would use openssl followed by perl, followed by uuencode. In terms of portability, I would use uuencode followed by Perl followed by openssl (If you care about reusing the code on as many other UNIX like stock platforms as possible). Be careful though because not all UNIX variants support the -m switch (iirc AIX does, HP/UX does, Solaris doesn't).

$ time perl -MMIME::Base64 -e 'undef $/;while(<>){print encode_base64($_);}' \
> out.jpg 1>filename.b64
real 0m0.025s
$ time uuencode -m -o filename.b64 out.jpg filename_when_uudecoded.txt
real 0m0.051s
$ time openssl base64 -in out.jpg -out filename.b64
real 0m0.017s

Use the -m switch to uuencode file_in.txt per base64 as specified by RFC1521 and write it to filename.b64 (with filename_when_uudecoded.txt as the default filename when decoded):

uuencode -m -o filename.b64 file_in.txt filename_when_uudecoded.txt

STDIN example:

cat file_in.txt | uuencode -m -o filename.b64 filename_when_uudecoded.txt
uuencode -m [-o output_file] [file] name

Where name is the name to display in the encoded header.

Example:

cat docbook-xsl.css | uuencode -m docbook-xsl.css

or

uuencode -m -o docbook-xsl.css.b64 docbook-xsl.css docbook-xsl.css
1

In addition to Steve Folly's answer above, when encoding in stdin mode, to avoid passing extra newlines, press CTRL+D twice to end input without any additional newlines.
Output will show right after the same line.

For example:

$ openssl base64 EnterinputCTRL+DCTRL+DaW5wdXQ=
$

Alternatively, you could use printf:

$ printf 'input' | openssl base64
aW5wdXQ=
$
2

For some reason, echo -n <data> | openssl base64 added a newline in the middle of my base64 data. I assume it was because my base64 data was really long.

Using echo -n <data> | base64 to encode and echo -n <base64-ed data> | base64 -D to decode worked fine.

1

A simple NodeJS version:

node -e "process.stdout.write(new Buffer(process.argv[1]).toString('base64'))" "Hello world!"
2

There is Perl plus MIME::Base64:

perl -MMIME::Base64 -e 'undef $/;while(<>){print encode_base64($_);}'

This comes pre-installed. You can specify separate files on the command line (or supply the data on standard input); each file is separately encoded. You can also do:

perl -i.txt -MMIME::Base64 -e 'undef $/;while(<>){print encode_base64($_);}' file1

This backs up file1 to file1.txt, and writes the Base-64 encoded output over the original file.

recode should do the trick for you

recode ../b64 < file.txt > file.b64

recode is available for OS X via MacPorts.

3

If you are base64 encoding a font file, you can do this:

base64 my-webfont.ttf > my-webfont.b64.ttf.txt

I use this on a Mac (10.10) all the time.

Note: There will be no linebreaks.

Cross-platform solutions

We compiled a list of cross-platform shell commands to encode a file as base64. The following commands take an input file (named deploy.key in examples) and convert it to base64 without any newline wrapping. The base64 output is printed to the terminal via stdout.

# For systems with openssl
openssl base64 -A -in=deploy.key
# For systems with Python (2 or 3) installed
python -c "import base64; print(base64.standard_b64encode(open('deploy.key', 'rb').read()).decode())"
# For Windows or Linux systems that have the GNU coreutils base64 command
base64 --wrap=1000000 deploy.key
# For macOS systems
base64 --break=1000000 deploy.key

To redirect the output to a file, append > base64-encoded.txt (using a file name of your choosing).

These commands were prototyped as part of this pull request where we wanted cross-platform shell commands to base64 encode an SSH private key to remove newlines.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like