Nathan Jones
Nathan Jones

Reputation: 5174

Why is the output of the linux base64 command different than other encoders?

If I run the command echo 1234 | base64, I get back MTIzNAo=. If I input the same string into other encoders (base64encode.org for example), I get different MTIzNA==.

I see a similar pattern with the differences between encoders. Both outputs seem to very similar, but the last or second to last character has differences.

echo 12345678 | base64 outputs MTIzNDU2NzgK, but on base64encode.org I get MTIzNDU2Nzg=

NodeJS's Buffer encoder to base64 also gives the same output as base64encode.org's encoder, so I'm assuming that something isn't right with how I'm using the base64 command in Linux.

What's the difference?

Upvotes: 0

Views: 848

Answers (2)

user3159253
user3159253

Reputation: 17455

Because there's a newline:

alex@galene ~ $ echo -n 1234 | base64
MTIzNA==

Upvotes: 2

Technologeeks
Technologeeks

Reputation: 7907

That's because echo has a newline, which gets encoded, but not when you put it into the various website or JS encoders (you're putting it into a string in JS, so no newline, or into a textbox in a website, so no newline, either). You can actually "see" the newline in the "o=" there (implies there's another character, as opposed to == which is two characters of padding).

Try "echo -n 1234" - you should get A==. The "-n" doesn't echo a newline.

Upvotes: 1

Related Questions