PHP’s base64_encode
is returning a different string to the linux base64
command.
Why is this?
PHP:
$ php
<?php
echo base64_encode('test');
?>
dGVzdA==
Linux base64:
$ echo 'test' | base64
dGVzdAo=
echo
usually outputs a new line character at the end of the string, to suppress that use the -n
switch:
$ echo -n 'test' | base64
dGVzdA==
Similarly for PHP:
$ php
<?php
echo base64_encode("test\n");
?>
dGVzdAo=
Answer:
open console in your browser, type atob('dGVzdAo=')
:
(source: gyazo.com)
You have extra character in your input. And that is 0x0A
(LF).
Answer:
When doing an echo it gives me this:
MacPro:~ bardiir$ echo 'test'
test
MacPro:~ bardiir$
I’d guess you might have an included line-ending in the unix one as echo is probably appending a newline character even if you pipe it throuch to the base64 encode.
Answer:
The linux base64
has a new line at the end.
Answer:
It is because Unix version encodes also the end of line.
To receive similar effect in PHP you will have to do something like that:
echo base64_encode('test'.PHP_EOL);
which will output:
dGVzdAo=
See the proof here: ideone.com/HorVD
EDIT: As Charles mentioned, PHP_EOL
is platform-specific, so to check the above on Windows you will have to replace it with Unix-like end of line symbol:
echo base64_encode("test\n");
Answer:
The same to above guys.
In mac os X, just test this :
$ echo 'test' | cat -e
test$
Or,
$ echo -n 'test' | cat -e
test
And,about the echo command, can see the tip: