Ruby 中的 base64 编码字符串中的奇怪 \n
Ruby 中内置的 Base64 库添加了一些 '\n'。我无法找出原因。对于这个特殊示例:
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'base64'
=> true
irb(main):003:0> str = "1110--ad6ca0b06e1fbeb7e6518a0418a73a6e04a67054"
=> "1110--ad6ca0b06e1fbeb7e6518a0418a73a6e04a67054"
irb(main):004:0> Base64.encode64(str)
=> "MTExMC0tYWQ2Y2EwYjA2ZTFmYmViN2U2NTE4YTA0MThhNzNhNmUwNGE2NzA1\nNA==\n"
\n 位于倒数最后一个和第六个位置。解码器(Base64.decode64)完美返回旧字符串。奇怪的是,这些 \n 不会为编码字符串添加任何值。当我从输出字符串中删除换行符时,解码器会再次完美地对其进行解码。
irb(main):005:0> Base64.decode64(Base64.encode64(str).gsub("\n", '')) == str
=> true
更重要的是,我使用另一个 JS 库来生成相同输入字符串的 base64 编码输出,输出不带 \n。
这是一个错误还是其他什么?以前有人遇到过这个问题吗?
供参考,
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
The inbuilt Base64 library in Ruby is adding some '\n's. I'm unable to find out the reason. For this special example:
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'base64'
=> true
irb(main):003:0> str = "1110--ad6ca0b06e1fbeb7e6518a0418a73a6e04a67054"
=> "1110--ad6ca0b06e1fbeb7e6518a0418a73a6e04a67054"
irb(main):004:0> Base64.encode64(str)
=> "MTExMC0tYWQ2Y2EwYjA2ZTFmYmViN2U2NTE4YTA0MThhNzNhNmUwNGE2NzA1\nNA==\n"
The \n's are at the last and 6th position from end. The decoder (Base64.decode64) returns back the old string perfectly. Strange thing is, these \n's don't add any value to the encoded string. When I remove the newlines from the output string, the decoder decodes it again perfectly.
irb(main):005:0> Base64.decode64(Base64.encode64(str).gsub("\n", '')) == str
=> true
More of this, I used an another JS library to produce the base64 encoded output of the same input string, the output comes without the \n's.
Is this a bug or anything else? Has anybody faced this issue before?
FYI,
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
编辑:自从我写了这个答案
Base64.strict_encode64()
,它不会添加换行符。文档有点令人困惑,
b64encode
方法应该为每 60 个字符添加一个换行符,而encode64
方法的示例实际上是使用b64encode
方法。看来
encode64
使用的 Array 类的pack("m")
方法也添加了换行符。我认为这是一个设计错误,这不是可选的。您可以自己删除换行符,或者如果您使用的是 Rails,则有 ActiveSupport::CoreExtensions::Base64::Encoding 使用
encode64s
方法。Edit: Since I wrote this answer
Base64.strict_encode64()
was added, which does not add newlines.The docs are somewhat confusing, the
b64encode
method is supposed to add a newline for every 60th character, and the example for theencode64
method is actually using theb64encode
method.It seems the
pack("m")
method for the Array class used byencode64
also adds the newlines. I would consider it a design bug that this is not optional.You could either remove the newlines yourself, or if you're using rails, there's ActiveSupport::CoreExtensions::Base64::Encoding with the
encode64s
method.在 ruby-1.9.2 中,您有 Base64.strict_encode64 ,它不会在末尾添加 \n (换行符)。
In ruby-1.9.2 you have Base64.strict_encode64 which doesn't add that \n (newline) at the end.
使用
strict_encode64
方法。encode64
每 60 个符号添加 \nUse
strict_encode64
method.encode64
adds \n every 60 symbols是的,这很正常。 doc 给出了一个示例,演示了该行 -分裂。 base64 在其他语言中也做同样的事情(例如 Python)。
在编码阶段添加与内容无关的换行符的原因是,base64 最初被设计为用于在电子邮件中发送二进制内容的编码机制,其中行长度受到限制。如果您不需要它们,请随意更换它们。
Yeah, this is quite normal. The doc gives an example demonstrating the line-splitting. base64 does the same thing in other languages too (eg. Python).
The reason content-free newlines are added at the encode stage is because base64 was originally devised as an encoding mechanism for sending binary content in e-mail, where the line length is limited. Feel free to replace them away if you don't need them.
似乎它们必须被剥离/忽略,例如:
Seems they've got to be stripped/ignored, like:
使用
Base64#encode64
时添加的\n
是正确的,查看这篇文章:https://glaucocustodio.github.io/2014/09/27/a-reminder-about-base64encode64-in-ruby/The
\n
added when usingBase64#encode64
is correct, check this post out: https://glaucocustodio.github.io/2014/09/27/a-reminder-about-base64encode64-in-ruby/