Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shell base64 encode result is different from others?

Tags:

base64

I'm to encode a string.

First I used shell command: $ echo "teststring" | base64 dGVzdHN0cmluZwo=

But when I use a online tool, http://www.motobit.com/util/base64-decoder-encoder.asp the result is: dGVzdHN0cmluZw==

Why are they different?

like image 280
lseeo Avatar asked Jul 05 '13 08:07

lseeo


People also ask

Is Base64 encoding always the same?

The short answer is yes, unique binary/hex values will always encode to a unique base64 encoded string. BUT, multiple base64 encoded strings may represent a single binary/hex value. This is because hex bytes are not aligned with base64 'digits'.

Why does Base64 strings end with ==?

The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.

Can Base64 encoding fail?

You can encode arbitrary bytes in base64 (which is why the encoding functions don't return errors). Only decoding can fail. The whole point of Base64 encoding is to take arbitrary bytes and reduce them to printable ASCII characters. There is no such thing as an invalid character for encoding, only for decoding.


1 Answers

echo outputs the string and a newline at the end. If you want just the string you provided, use echo -n

like image 76
viraptor Avatar answered Oct 10 '22 22:10

viraptor