Dave
Dave

Reputation: 19250

Why is base64 encoding my file on a Mac not resulting in a proper base64 string?

I'm using Mac OS 10.13.3. I'm trying to base64 encode a binary file but am having some issues. Specifically, I thought all base64 encoded files have to have a length that is a multiple of 4. However, when I encode my file, notice taht the length is not divisible by 4 ...

localhost:lib davea$ openssl base64 -in myfile.binary -out ~/Downloads/myfile.base64
localhost:lib davea$ ls -al ~/Downloads/myfile.base64 
-rw-r--r-- 1 davea staff 93162 May 31 14:22 /Users/davea/Downloads/myfile.base64

Also when I look at the contents of the base64 file, I don't see the traditional "=" or "==" at the end, which usually indicates padding

localhost:lib davea$ cat ~/Downloads/myfile.base64
...
C9vgMjoKSQYkXMLTrGKRleR558g3bY3VTqlsVvTqZXquCLp4JS4cprTG6N10H0u9
i4pwPrVmSAP2DmE1V7mGwR2e4fiYEWnZjpSbHofpzlUo34yhiQ2/5kJoQZktD7BU
uxYBAgQIECBAgBs2

Am I doing something wrong, or is there another way to base64 encode my file?

Upvotes: 5

Views: 12154

Answers (2)

CRD
CRD

Reputation: 53010

Am I doing something wrong,

No.

or is there another way to base64 encode my file?

Yes, you can use base64. It takes a parameter to specify line length but is otherwise similar, the equivalent to your command is:

base64 -b 64 -i myfile.binary -o ~/Downloads/myfile.base64

Also when I look at the contents of the base64 file, I don't see the traditional "=" or "==" at the end, which usually indicates padding

Base64 maps 3 input bytes to 4 output bytes. Your file is 93162 bytes which is divisible by 3, so no padding required.

Upvotes: 1

Jason
Jason

Reputation: 2671

OK. I believe we were over thinking this quite a bit. Here is what you are looking for to get the desired behavior:

openssl base64 -A -in myfile.binary -out ~/Downloads/myfile.base64

This will convert to base64 without any line endings. The -A option is what does the trick.

Upvotes: 7

Related Questions