module ActiveRecordExtension

Public Instance Methods

decrypt(data, key, iv) click to toggle source

Returns data decrypted with AES-256-CBC

# File lib/active_record_extension.rb, line 15
def decrypt(data, key, iv)
  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  cipher.decrypt
  cipher.key = key
  cipher.iv  = iv
  cipher.update(data) + cipher.final
end
encrypt(data, key, iv) click to toggle source

Returns binary data encrypted with AES-256-CBC

# File lib/active_record_extension.rb, line 5
def encrypt(data, key, iv)
  return nil if data.blank?
  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  cipher.encrypt
  cipher.key = key
  cipher.iv  = iv
  cipher.update(data) + cipher.final
end