奇怪\\ n用base64连接在Ruby中codeD字符串 [英] Strange \n in base64 encoded string in Ruby

查看:330
本文介绍了奇怪\\ n用base64连接在Ruby中codeD字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中内置的Base64编码库增加了一些的'\\ n的。我无法找出原因。对于这个特殊的例子:

  IRB(主):001:0>需要'RubyGems的'
= GT;真正
IRB(主):002:0>要求的base64
= GT;真正
IRB(主):003:0>海峡=1110 - ad6ca0b06e1fbeb7e6518a0418a73a6e04a67054
= GT; 1110 - ad6ca0b06e1fbeb7e6518a0418a73a6e04a67054
IRB(主):004:0> Base64.en code64(STR)
= GT; MTExMC0tYWQ2Y2EwYjA2ZTFmYmViN2U2NTE4YTA0MThhNzNhNmUwNGE2NzA1 \\ NNA == \\ n

成\\ n的是从末最后和第六的位置。去codeR(Base64.de code64)完全返回回到老的字符串。奇怪的是,这些\\ n的不增加任何价值的EN codeD字符串。当我从输出字符串中删除换行符,去codeR德codeS再次完美。

  IRB(主):005:0> Base64.de code64(Base64.en code64(STR).gsub(\\ n,''))==海峡
= GT;真正

更多的这一点,我用了一个另一个JS库产生的base64连接相同的输入字符串的codeD输出,输出是不带\\ n的。

这是一个错误或别的什么吗?有没有人之前遇到这个问题?

仅供参考,

  $红宝石-v
红宝石1.8.7(2008-08-11补丁级别72)的i486-linux下]


解决方案

编辑:,因为我写这个答案 Base64.strict_en code64()添加,不添加新行。


是有些混乱的文档中, b64en code 方法应该添加一个新行每60字符,并为连接code64 方法的例子实际上是使用 b64en code 方法。

看来包(M)连接code64 使用Array类的方法还增加了换行。我会认为这是一个错误的设计,这是不可选的。

您既可以自己删除换行符,或者如果你使用的铁轨,有<一个href=\"http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Base64/Encoding.html\">ActiveSupport::CoreExtensions::Base64::Encoding与连接code64s 方法。

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]

解决方案

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 the encode64 method is actually using the b64encode method.

It seems the pack("m") method for the Array class used by encode64 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.

这篇关于奇怪\\ n用base64连接在Ruby中codeD字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆