use ActiveSupport's Base64 encoding methods instead of calling Array#pack directly; strip new lines.

This commit is contained in:
danielgrippi 2011-08-31 16:52:12 -07:00 committed by Ilya Zhitomirskiy
parent 36f5e45c25
commit ce9c17fa2e
2 changed files with 3 additions and 19 deletions

View file

@ -16,7 +16,7 @@ module Encryptor
cipher = OpenSSL::Cipher.new('AES-256-CBC') cipher = OpenSSL::Cipher.new('AES-256-CBC')
key = cipher.random_key key = cipher.random_key
iv = cipher.random_iv iv = cipher.random_iv
{'key' => Base64.encode64s(key), 'iv' => Base64.encode64(iv)} {'key' => Base64.encode64s(key), 'iv' => Base64.encode64s(iv)}
end end
def aes_encrypt(txt, key) def aes_encrypt(txt, key)

View file

@ -5,28 +5,12 @@
# Add URL safe Base64 support # Add URL safe Base64 support
module Base64 module Base64
module_function module_function
# Returns the Base64-encoded version of +bin+.
# This method complies with RFC 4648.
# No line feeds are added.
def strict_encode64(bin)
[bin].pack("m0")
end
# Returns the Base64-decoded version of +str+.
# This method complies with RFC 4648.
# ArgumentError is raised if +str+ is incorrectly padded or contains
# non-alphabet characters. Note that CR or LF are also rejected.
def strict_decode64(str)
Rails.logger.info("trying to decode string: " + str)
str.unpack("m0").first
end
# Returns the Base64-encoded version of +bin+. # Returns the Base64-encoded version of +bin+.
# This method complies with ``Base 64 Encoding with URL and Filename Safe # This method complies with ``Base 64 Encoding with URL and Filename Safe
# Alphabet'' in RFC 4648. # Alphabet'' in RFC 4648.
# The alphabet uses '-' instead of '+' and '_' instead of '/'. # The alphabet uses '-' instead of '+' and '_' instead of '/'.
def urlsafe_encode64(bin) def urlsafe_encode64(bin)
strict_encode64(bin).tr("+/", "-_") self.encode64s(bin).tr("+/", "-_")
end end
# Returns the Base64-decoded version of +str+. # Returns the Base64-decoded version of +str+.
@ -34,7 +18,7 @@ module Base64
# Alphabet'' in RFC 4648. # Alphabet'' in RFC 4648.
# The alphabet uses '-' instead of '+' and '_' instead of '/'. # The alphabet uses '-' instead of '+' and '_' instead of '/'.
def urlsafe_decode64(str) def urlsafe_decode64(str)
strict_decode64(str.tr("-_", "+/")) self.decode64(str.tr("-_", "+/"))
end end
end end