当前位置:首页 > 开发 > 编程语言 > Ruby > 正文

ruby的base64编码的诟病

发表于: 2014-07-01   作者:counsellor   来源:转载   浏览:
摘要: str_raw = "To be prepared is half the victory." str_encoded = [str_raw].pack('m')  或者 ["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m") => &q
str_raw = "To be prepared is half the victory."
str_encoded = [str_raw].pack('m')

 或者

["abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"].pack("m")
=> "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJT\nVFVWV1hZWg==\n"

 

上面两段代码是ruby提供的base64编码函数,很方便。

 

不过对于某些特殊需求的人,这个函数似乎是有缺陷的。

 

第一类人-只想要编码的串不需要换行:

       请使用

               # File base64.rb, line 64
def strict_encode64(bin)
  [bin].pack("m0")
end

 官方说明:

Returns the Base64-encoded version of bin. This method complies with RFC 4648. No line feeds are added.

 

第二类人-编码和换行都要:

       请使用

               # File base64.rb, line 37
def encode64(bin)
  [bin].pack("m")
end

 官方说明:

Returns the Base64-encoded version of bin. This method complies with RFC 2045. Line feeds are added to every 60 encoded characters.

 

好,工作结束,大家都很happy~赶紧回家吃饭吧~

 

且慢,少侠请留步,其实不是这样的。

带换行的形式,官网说“This method complies with RFC 2045”。是这样吗?

 

https://tools.ietf.org/html/rfc2045#section-6.8 写道
Freed & Borenstein Standards Track [Page 25]

RFC 2045 Internet Message Bodies November 1996


such assurance is possible, however, when the number of octets
transmitted was a multiple of three and no "=" characters are
present.

Any characters outside of the base64 alphabet are to be ignored in
base64-encoded data.

Care must be taken to use the proper octets for line breaks if base64
encoding is applied directly to text material that has not been
converted to canonical form. In particular, text line breaks must be
converted into CRLF sequences prior to base64 encoding. The
important thing to note is that this may be done directly by the
encoder rather than in a prior canonicalization step in some
implementations.

NOTE: There is no need to worry about quoting potential boundary
delimiters within base64-encoded bodies within multipart entities
because no hyphen characters are used in the base64 encoding.

 

的确,在RFC 2045中说MIME的base64编码是60个字符一个line feed。但是line feed并不是<LF>而是<CRLF>。在wiki中也会发现PEMRFC 2045RFC 4880等base64编码的 line feed也都是<CRLF>,所以ruby官方的说明并不完全正确。

 

正确的方式是:

def encode64(bin)
  [bin].pack("m").gsub(/\n/,"\r\n")
end

 

当然代码没有验证,我没逗你。好久不用ruby了,python比较合手。

忘不了第三类人的-要url的base64编码:

               # File base64.rb, line 80
def urlsafe_encode64(bin)
  strict_encode64(bin).tr("+/", "-_")
end
            

 官方说明:

Returns the Base64-encoded version of bin. This method complies with “Base 64 Encoding with URL and Filename Safe Alphabet” in RFC 4648. The alphabet uses ‘-’ instead of ‘+’ and ‘_’ instead of ‘/’.

 

 

 

 

ruby的base64编码的诟病

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号