Module: Cryptonite

Extended by:
ActiveSupport::Concern
Defined in:
lib/cryptonite.rb,
lib/cryptonite/version.rb

Overview

Cryptonite

Enables the encryption of specific ActiveRecord attributes.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PUBLIC_KEY =
OpenSSL::PKey::RSA.new(File.read(ENV['PUBLIC_KEY_FILE'])) rescue nil
PRIVATE_KEY =
OpenSSL::PKey::RSA.new(File.read(ENV['PRIVATE_KEY_FILE']), ENV['PRIVATE_KEY_PASSWORD']) rescue nil
VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#read_attribute(attr_name) ⇒ Object

Wrap read_attribute to encrypt value.



48
49
50
51
52
53
54
55
56
57
# File 'lib/cryptonite.rb', line 48

def read_attribute(attr_name)
  attr_name = attr_name.to_s

  if self.class.encrypted_attributes.include?(attr_name)
    value = super(attr_name)
    decrypt(value) unless value.nil?
  else
    super(attr_name)
  end
end

#write_attribute(attr_name, value) ⇒ Object

Wrap write_attribute to encrypt value.



37
38
39
40
41
42
43
44
45
# File 'lib/cryptonite.rb', line 37

def write_attribute(attr_name, value)
  attr_name = attr_name.to_s

  if self.class.encrypted_attributes.include?(attr_name)
    value = encrypt(value)
  end unless value.nil?

  super(attr_name, value)
end