Class: MySQLEncryptor

Inherits:
Object
  • Object
show all
Includes:
MySQLEncryption, Singleton
Defined in:
lib/attr_encryption/mysql_encryptor.rb

Instance Method Summary collapse

Methods included from MySQLEncryption

#mysql_decrypt, #mysql_encrypt

Instance Method Details

#decrypt(encrypt_options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



5
6
7
8
# 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) ⇒ Object



30
31
32
33
# 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