class EasyAES

Public Class Methods

decrypt(data, secret) click to toggle source
# File lib/easy_aes.rb, line 39
def self.decrypt data, secret
  # create the cipher for encrypting
  cipher = OpenSSL::Cipher::Cipher.new 'aes-256-cbc'
  cipher.decrypt

  # load secret into the cipher
  secret = deserialize secret
  cipher.key = secret.first
  cipher.iv = secret.last

  # decrypt the message
  data = deserialize data
  decrypted = cipher.update data
  decrypted << cipher.final

  decrypted
end
deserialize(data) click to toggle source
# File lib/easy_aes.rb, line 18
def self.deserialize data
  YAML.load Base64.decode64(data)
end
encrypt(data, secret) click to toggle source
# File lib/easy_aes.rb, line 22
def self.encrypt data, secret
  # create the cipher for encrypting
  cipher = OpenSSL::Cipher::Cipher.new 'aes-256-cbc'
  cipher.encrypt

  # load secret into the cipher
  secret = deserialize secret
  cipher.key = secret.first
  cipher.iv = secret.last

  # encrypt the message
  encrypted = cipher.update data
  encrypted << cipher.final

  serialize encrypted
end
generate_secret() click to toggle source
# File lib/easy_aes.rb, line 9
def self.generate_secret
  cipher = OpenSSL::Cipher::Cipher.new 'aes-256-cbc'
  serialize [cipher.random_key, cipher.random_iv]
end
serialize(data) click to toggle source
# File lib/easy_aes.rb, line 14
def self.serialize data
  Base64.encode64 data.to_yaml
end