Module: ActiveUrl::Crypto

Defined in:
lib/active_url/crypto.rb

Constant Summary collapse

CipherError =
OpenSSL::Cipher.const_defined?(:CipherError) ? OpenSSL::Cipher::CipherError : OpenSSL::CipherError
PADDING =
{ 2 => "==", 3 => "=" }

Class Method Summary collapse

Class Method Details

.decrypt(b64) ⇒ Object



18
19
20
21
22
23
# File 'lib/active_url/crypto.rb', line 18

def self.decrypt(b64)
  cipher = Base64.decode64("#{b64.gsub("-", "+").gsub("_", "/")}#{PADDING[b64.length % 4]}")
  crypto = start(:decrypt)
  clear = crypto.update(cipher)
  clear << crypto.final
end

.encrypt(clear) ⇒ Object



11
12
13
14
15
16
# File 'lib/active_url/crypto.rb', line 11

def self.encrypt(clear)
  crypto = start(:encrypt)
  cipher = crypto.update(clear)
  cipher << crypto.final
  Base64.encode64(cipher).gsub(/[\s=]+/, "").gsub("+", "-").gsub("/", "_")
end