2008-02-07 16:50:46 -05:00
|
|
|
begin
|
|
|
|
require 'base64'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2007-12-18 16:26:01 -05:00
|
|
|
module ActiveSupport
|
|
|
|
if defined? ::Base64
|
|
|
|
Base64 = ::Base64
|
|
|
|
else
|
2010-08-14 01:13:00 -04:00
|
|
|
# Base64 provides utility methods for encoding and de-coding binary data
|
2008-10-05 17:16:26 -04:00
|
|
|
# using a base 64 representation. A base 64 representation of binary data
|
|
|
|
# consists entirely of printable US-ASCII characters. The Base64 module
|
|
|
|
# is included in Ruby 1.8, but has been removed in Ruby 1.9.
|
2007-12-18 16:26:01 -05:00
|
|
|
module Base64
|
2008-10-05 17:16:26 -04:00
|
|
|
# Encodes a string to its base 64 representation. Each 60 characters of
|
|
|
|
# output is separated by a newline character.
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# ActiveSupport::Base64.encode64("Original unencoded string")
|
2008-10-05 17:16:26 -04:00
|
|
|
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n"
|
2007-12-18 16:26:01 -05:00
|
|
|
def self.encode64(data)
|
|
|
|
[data].pack("m")
|
|
|
|
end
|
|
|
|
|
2008-10-05 17:16:26 -04:00
|
|
|
# Decodes a base 64 encoded string to its original representation.
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==")
|
2008-10-05 17:16:26 -04:00
|
|
|
# # => "Original unencoded string"
|
2007-12-18 16:26:01 -05:00
|
|
|
def self.decode64(data)
|
|
|
|
data.unpack("m").first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-28 19:48:14 -05:00
|
|
|
|
|
|
|
# Encodes the value as base64 without the newline breaks. This makes the base64 encoding readily usable as URL parameters
|
|
|
|
# or memcache keys without further processing.
|
|
|
|
#
|
|
|
|
# ActiveSupport::Base64.encode64s("Original unencoded string")
|
|
|
|
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw=="
|
|
|
|
def Base64.encode64s(value)
|
|
|
|
encode64(value).gsub(/\n/, '')
|
|
|
|
end
|
2007-12-18 16:26:01 -05:00
|
|
|
end
|