Sha256: 7a37484e02ff0f3a149aef13ae7c689c9ec82910c9e479a2ace8eb4878dc7e82

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require "acts_as_encryptable/version"

module ActsAsEncryptable
  def acts_as_encryptable column_name, key, salt
    send :define_method, column_name do
      encryptor = "@encryptor_#{key}_#{salt}"

      instance_variable_set(
        encryptor,
        ActiveSupport::MessageEncryptor.new(
          ActiveSupport::KeyGenerator.new(send key).generate_key(send(salt), 32)
        )
      ) unless instance_variable_defined? encryptor

      encryptor = instance_variable_get(encryptor)
      encryptor.decrypt_and_verify(read_attribute column_name)
    end

    send :define_method, "#{column_name}=".to_sym do |value|
      encryptor = "@encryptor_#{key}_#{salt}"

      instance_variable_set(
        encryptor,
        ActiveSupport::MessageEncryptor.new(
          ActiveSupport::KeyGenerator.new(send key).generate_key(send(salt), 32)
        )
      ) unless instance_variable_defined? encryptor

      encryptor = instance_variable_get(encryptor)
      write_attribute column_name, encryptor.encrypt_and_sign(value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_encryptable-0.1.1 lib/acts_as_encryptable.rb