Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
2  
Be aware that although the base64-encoding syntax is consistent from one OS to another, the base64-decoding syntax is either base64 -d or base64 -D depending on your operating system. OSX uses -D. –  Chris Johnson Nov 30 '12 at 22:06

9 Answers 9

up vote 69 down vote accepted

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

share|improve this answer
    
Perfect! Thanks. –  Josh Mar 17 '10 at 0:30
23  
Use openssl base64 < path/to/file.png | tr -d '\n' | pbcopy or cat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy to skip writing to a file and just copy the base64-encoded output to the clipboard without the line breaks. –  Mathias Bynens Apr 12 '11 at 13:07
    
And use a -d flag to decode. –  kenny Dec 11 '13 at 22:26
    
In mac, To base64 encode a string: openssl base64 -e <<< ram and to decode: openssl base64 -d <<< cmFtCg== –  Ram swaroop Nov 13 at 10:52

recode should do the trick for you

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

Here is the darwinports page for recode if its not already available.

share|improve this answer
    
There's nothing built in? –  Josh Mar 17 '10 at 0:24
    
@Josh - there is - openssl –  Steve Folly Mar 17 '10 at 0:25

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.

share|improve this answer

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>
share|improve this answer
6  
The "echo" solution adds a LF (line feed) char to the end of the input string, though. Better use: echo -n 'input' –  SuperTempel Jul 17 '12 at 15:17
2  
And even a bit more succinct openssl base64 <<< input –  Garrett Fogerlie Jun 6 '13 at 16:57

Let me expand on Steve Folly's answer:

$ openssl base64 -in image.ext -out image_base64.txt

Just an easier-to-use representation.

share|improve this answer

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
share|improve this answer
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
share|improve this answer

Python comes preinstalled on all macs nowadays.

Assuming you have Python console (or ipython) running.

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.