Why is there an inconsistency in base64 output?

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

Example

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
neo4j:@N

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
bmVvNGo6QE4K
$ echo 'bmVvNGo6QE4K' | base64 --decode
neo4j:@N

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


You are encoding (slightly) different strings:

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

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              
bmVvNGo6QE4=