为什么这些Base64编码的输出在相同的输入字符串下会有所不同?

huangapple go评论93阅读模式
英文:

Why do these base64 encoding outputs differ, given the same input string?

问题

当从相同的输入字符串获取Base64编码的字符串时,我发现JavaScript、Groovy和Go得到的结果是相同的,但GNU base64稍有不同。为什么会这样呢?

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=

答案1

得分: 7

echo 'Laurence Tureaud is Mr. T'的翻译结果如下:

回声在字符串后添加一个换行符。

尝试以下命令以删除换行符:

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

你将得到 TGF1cmVuY2UgVHVyZWF1ZCBpcyBNci4gVA==

英文:

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

答案2

得分: 4

所有的输出都是相同的。

唯一的区别是,当使用 echo 命令时,Bash 会在末尾添加一个换行符 (\n)。因此,输出中会多出一个附加字符(= 字符只是 base64 编码中的填充字符)。

英文:

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

huangapple
  • 本文由 发表于 2015年5月7日 21:26:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/30102466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定