最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why do these base64 encoding outputs differ, given the same input string? - Stack Overflow

programmeradmin6浏览0评论

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=

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=
Share Improve this question edited May 7, 2015 at 13:34 Uwe Plonus 9,9844 gold badges45 silver badges49 bronze badges asked May 7, 2015 at 13:26 user605331user605331 3,7884 gold badges37 silver badges60 bronze badges 3
  • Are they using the same encoding? – brso05 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. – Daniel Tung Commented May 7, 2015 at 13:32
  • 4 try echo -n 'Laurence Tureaud is Mr. T' | base64 – Pointy Commented May 7, 2015 at 13:35
Add a ment  | 

2 Answers 2

Reset to default 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==

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).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论