I just spent way too much time banging my head against an SMTP server because it didn't like the base64 encoded credentials I was using. Turns out that when I chose NOT to use perl
like so many instructions on the internet say to use, I was making a big mistake. Why is this? I thought base64 was a single standard.
Consider:
$ perl -MMIME::Base64 -e 'print encode_base64("ASDF1234asdf")'
QVNERjEyMzRhc2Rm
$ base64 <<<"ASDF1234asdf"
QVNERjEyMzRhc2RmCg==
$ python3.6 -m base64 <<<"ASDF1234asdf"
QVNERjEyMzRhc2RmCg==
$ python2.7 -m base64 <<<"ASDF1234asdf"
QVNERjEyMzRhc2RmCg==
$ perl -MMIME::Base64 -e "print encode_base64('my_user_name@my_domain.com')"
bXlfdXNlcl9uYW1lQG15X2RvbWFpbi5jb20=
$ base64 <<<"my_user_name@my_domain.com"
bXlfdXNlcl9uYW1lQG15X2RvbWFpbi5jb20K
So, perl
is unique in its output and my server requires it that way.
Why am I getting different results?
How do you get the MIME/SMTP friendly output with something other than perl?
This is not a Perl vs. everyone else thing; this is a "using Bash's <<<
construct" vs. "not doing that" thing. Though not explicitly documented (at least not in the manpage on Ubuntu Xenial), Bash appends a newline to herestrings (the <<<
thing) when passing them to commands. As a result, all of the commands you're invoking with <<<
are actually encoding 'ASDF1234asdf\n'
, while Perl (which isn't invoked with a herestring) is encoding just 'ASDF1234asdf'
. Different input, different output.
In order to pass a string without a trailing newline to a command's standard input, use the printf
command, e.g.:
$ printf %s ASDF1234asdf | base64
QVNERjEyMzRhc2Rm
There are no different base64 encodings, but encoding a newline character at the end, or not:
$ perl -MMIME::Base64 -e 'print encode_base64("ASDF1234asdf")'
QVNERjEyMzRhc2Rm
$ perl -MMIME::Base64 -e 'print encode_base64("ASDF1234asdf\n")'
QVNERjEyMzRhc2RmCg==
$ echo -ne "my_user_name@my_domain.com" | base64
bXlfdXNlcl9uYW1lQG15X2RvbWFpbi5jb20=
$ echo -ne "my_user_name@my_domain.com\n" | base64
bXlfdXNlcl9uYW1lQG15X2RvbWFpbi5jb20K
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With