1

When getting a bas64 encoded string from the same input string I find that JavaScript, Groovy, and Go have the same result, but GNU base64 is slightly different. Why is that?

JavaScript (nodejs v0.10.33):

new Buffer('Laurence Tureaud is Mr. T').toString('base64');
TGF1cmVuY2UgVHVyZWF1ZCBpcyBNci4gVA==

Groovy (2.3.7 on Java 8):

'Laurence Tureaud is Mr. T'.bytes.encodeBase64().toString()
TGF1cmVuY2UgVHVyZWF1ZCBpcyBNci4gVA==

Go (1.4):

b64.StdEncoding.EncodeToString([]byte("Laurence Tureaud is Mr. T"))
TGF1cmVuY2UgVHVyZWF1ZCBpcyBNci4gVA==

GNU base64 (GNU coreutils 8.12.197-032bb with UTF-8 term charset):

echo 'Laurence Tureaud is Mr. T' | base64
TGF1cmVuY2UgVHVyZWF1ZCBpcyBNci4gVAo=
Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
user605331
  • 3,718
  • 4
  • 33
  • 60

2 Answers2

7

echo 'Laurence Tureaud is Mr. T'

Echo adds a newline after the string.

Try the following to remove the newline:

echo -n 'Laurence Tureaud is Mr. T' | base64

And you get TGF1cmVuY2UgVHVyZWF1ZCBpcyBNci4gVA==

Daniel Tung
  • 427
  • 2
  • 14
Roger Gustavsson
  • 1,689
  • 10
  • 20
4

All output is the same.

The only difference is that bash appends a newline (\n) to the end when using echo. Therefore the is an additional character appended to the output (the character = is only a padding in base64).

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48