Command-Line – Why is There an Inconsistency in Base64 Output

command lineneo4jpassword

I am using the java based neo4j graph database on lubuntu 15.04.

The neo4j HTTP authentication header uses base64 encoding of 'username:password' (not including quotes). Using wireshark I can see the base64 code generated by neo4j.

However if I use the ubuntu coreutils base64 to encode the same string I get a slightly different encoding. This encoding is not accepted by neo4j.

Both encodings decode to the correct username:password string


username=neo4j and password=@N

Neo4j gives the encoded value of neo4j:@N as bmVvNGo6QE4= which decodes to neo4j:@N as expected

$ echo 'bmVvNGo6QE4=' | base64 --decode

Ubuntu coreutils base64 returns the encoded value of neo4j:@N as bmVvNGo6QE4K (which differs in the last character) but still decodes correctly;

$ echo 'neo4j:@N' | base64
$ echo 'bmVvNGo6QE4K' | base64 --decode

Why is this? What do I need to do get consistent encoding?

Best Answer

You are encoding (slightly) different strings:

$ echo 'bmVvNGo6QE4=' | base64 --decode | od -c
0000000   n   e   o   4   j   :   @   N
$ echo 'neo4j:@N' | od -c
0000000   n   e   o   4   j   :   @   N  \n

echo adds a trailing newline character. This leads to differing encodings.

Use printf instead, whose output specification is more exact:

$ printf '%s' 'neo4j:@N' | base64              
Related Question