Sha256: d3158d2b22238a8d4a1d6541d577c7d064060b93dd79438023b4eb307967d586

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

class MySQLEncryptor
  include MySQLEncryption
  include Singleton

  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

  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

  def encrypt_date(value)
    dt = value.is_a?(String) ? Date.safe_parse(value) : value
    dt.strftime("%Y%m%d") if dt.present?
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
attr_encryption-0.4.3 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.4.2 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.4.1 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.4.0 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.3.1 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.2.1 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.2.0 lib/attr_encryption/mysql_encryptor.rb
attr_encryption-0.1.2 lib/attr_encryption/mysql_encryptor.rb