class MySQLEncryptor

Public Instance Methods

decrypt(encrypt_options) click to toggle source
# File lib/attr_encryption/mysql_encryptor.rb, line 10
def decrypt(encrypt_options)
  decrypted_value = mysql_decrypt(encrypt_options[:value], encrypt_options[:key])
  return decrypted_value if decrypted_value.nil?

  case encrypt_options[:type]
  when 'text'
    decrypted_value.force_encoding('utf-8')
  when 'date'
    Date.parse(decrypted_value)
  when 'datetime'
    DateTime.parse(decrypted_value)
  when 'binary'
    decrypted_value # no processing
  when 'json'
    JSON.parse(decrypted_value)
  else
    raise "Invalid type specified for post-processing decrypted value"
  end
end
encrypt(encrypt_options) click to toggle source
# File lib/attr_encryption/mysql_encryptor.rb, line 5
def encrypt(encrypt_options)
  value_to_encrypt = encrypt_options[:value].nil? ? nil : encrypt_options[:type] == 'date' ? encrypt_date(encrypt_options[:value]) : encrypt_options[:value].to_s
  mysql_encrypt(value_to_encrypt, encrypt_options[:key])
end
encrypt_date(value) click to toggle source
# File lib/attr_encryption/mysql_encryptor.rb, line 30
def encrypt_date(value)
  dt = value.is_a?(String) ? Date.safe_parse(value) : value
  dt.strftime("%Y%m%d") if dt.present?
end