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

link|improve this question

79% accept rate
feedback

4 Answers

up vote 17 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

link|improve this answer
Perfect! Thanks. – Josh Mar 17 '10 at 0:30
3  
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
feedback

Openssl can be used more succinctly:

echo 'input' | openssl base64

or

openssl base64 <ENTER> [type input] <CTRL+D>
link|improve this answer
feedback

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.

link|improve this answer
feedback

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.

link|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
feedback

Your Answer

 
or
required, but never shown

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