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”。是这样吗?
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中也会发现PEM,RFC 2045,RFC 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 ‘/’.