Module: Lucifer::ClassMethods

Defined in:
lib/lucifer.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) encrypt_attributes(options = {})



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lucifer.rb', line 7

def encrypt_attributes(options = {})
  return if self.included_modules.include? Lucifer::InstanceMethods
  __send__ :include, Lucifer::InstanceMethods
  
  cattr_accessor :encrypted_columns, :decrypted_columns, :key, :suffix, :key_file
  
  self.suffix   = options[:suffix]   || '_b'
  self.key_file = options[:key_file] || 'key.yml'
  
  self.encrypted_columns = columns.select{|col| col.type == :binary && col.name.ends_with?(suffix)}.collect(&:name)  
  self.decrypted_columns = encrypted_columns.collect{|col| col.chomp suffix }
  decrypted_columns.each { |col| attr_accessor col }
  
  before_save :encrypt_columns
  
  secret   = YAML.load_file("#{Rails.root}/config/#{key_file}")[Rails.env].symbolize_keys
  self.key = EzCrypto::Key.with_password secret[:key], secret[:salt]
end

- (Object) key_decrypt(value)



30
31
32
33
34
# File 'lib/lucifer.rb', line 30

def key_decrypt(value)
  key.decrypt value
rescue # OpenSSL::CipherError # Specifying this class led to errors with Rails 2.3.4
  return nil
end

- (Object) key_encrypt(value)



26
27
28
# File 'lib/lucifer.rb', line 26

def key_encrypt(value)
  key.encrypt value
end