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=
3
  • Are they using the same encoding? Commented May 7, 2015 at 13:27
  • 3
    If you decode the results you'll see that the GNU base64 has a new line character at the end. (You get 'Laurence Tureaud is Mr. T\n' where \n is a new line. Commented May 7, 2015 at 13:32
  • 4
    try echo -n 'Laurence Tureaud is Mr. T' | base64 Commented May 7, 2015 at 13:35

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, yes, that was it indeed. The following matches: printf 'Laurence Tureaud is Mr. T' | base64
@user605331 If this worked for you, please mark this as the correct solution. Thanks! :)
SO won't allow until 5 minutes has passed. I tried.
@user605331 Sorry, didn't mean to rush you. Just wanted to put a little reminder. I added a little more to this answer for future reference!
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.