doutuo1908 2015-05-07 13:26
浏览 980
已采纳

给定相同的输入字符串,为什么这些base64编码输出不同?

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=
  • 写回答

2条回答 默认 最新

  • dongluan6784 2015-05-07 13:31
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongying6896 2015-05-07 13:32
    关注

    All output is the same.

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

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TensorFlow深度学习拓展项目
  • ¥20 springboot博客系统
  • ¥15 关于#c++#的问题:在A和B类中应该怎么定义复制构造函数来避开unordered_set复制构造的问题(相关搜索:头文件)
  • ¥15 MICE包多重插补后数据集汇总导出
  • ¥15 一道算法分析问题(关于3-MSAT)
  • ¥15 C++ FLUENT 化学反应速率 编写困难
  • ¥15 Python嵌套交叉验证
  • ¥15 linuxkit+elasticsearch
  • ¥15 兄得萌6.13do题😭😭大一小东西的work
  • ¥15 投不到原始数据,gdal投影代码