use ActiveSupport's Base64 encoding methods instead of calling Array#pack directly; strip new lines.
This commit is contained in:
parent
36f5e45c25
commit
ce9c17fa2e
2 changed files with 3 additions and 19 deletions
|
|
@ -16,7 +16,7 @@ module Encryptor
|
|||
cipher = OpenSSL::Cipher.new('AES-256-CBC')
|
||||
key = cipher.random_key
|
||||
iv = cipher.random_iv
|
||||
{'key' => Base64.encode64s(key), 'iv' => Base64.encode64(iv)}
|
||||
{'key' => Base64.encode64s(key), 'iv' => Base64.encode64s(iv)}
|
||||
end
|
||||
|
||||
def aes_encrypt(txt, key)
|
||||
|
|
|
|||
|
|
@ -5,28 +5,12 @@
|
|||
# Add URL safe Base64 support
|
||||
module Base64
|
||||
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+.
|
||||
# This method complies with ``Base 64 Encoding with URL and Filename Safe
|
||||
# Alphabet'' in RFC 4648.
|
||||
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
|
||||
def urlsafe_encode64(bin)
|
||||
strict_encode64(bin).tr("+/", "-_")
|
||||
self.encode64s(bin).tr("+/", "-_")
|
||||
end
|
||||
|
||||
# Returns the Base64-decoded version of +str+.
|
||||
|
|
@ -34,7 +18,7 @@ module Base64
|
|||
# Alphabet'' in RFC 4648.
|
||||
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
|
||||
def urlsafe_decode64(str)
|
||||
strict_decode64(str.tr("-_", "+/"))
|
||||
self.decode64(str.tr("-_", "+/"))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue