Module: ActiveCrypto::ClassMethods

Defined in:
lib/active_crypto.rb

Overview

Usage is very simple. You will generally only need the two class methods listed here in your ActiveRecord class model.

License

ActiveCrypto and EzCrypto are released under the MIT license.

Support

To contact the author, send mail to [email protected]

Also see my blogs at: stakeventures.com and neubia.com

This project was based on code used in my project StakeItOut, where you can securely share web services with your partners. stakeitout.com

© 2005 Pelle Braendgaard

Constant Summary collapse

@@session_keys =
{}

Instance Method Summary collapse

Instance Method Details

#clear_session_keysObject

Clears the session_key array. Generally this is handled automatically as a filter in ActionController. Only use these if you need to do something out of the ordinary.



101
102
103
# File 'lib/active_crypto.rb', line 101

def clear_session_keys() #:nodoc:
  @@session_keys.clear
end

#encrypt(*attributes) ⇒ Object

Turn encryption on for this record. List all encrypted attributes

class Document < ActiveRecord::Base

encrypt :title,:body end

Options are:

<tt>key</tt> - to specify an external KeyHolder, which holds the key used for encrypting and decrypting
<tt>base64</tt> - set to true in order to base64 encode the encrypted attributes.  defaults to false

class Document < ActiveRecord::Base
	belongs_to :user
	encrypt :title,:body,:key=>:user, :base64 => true
end


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/active_crypto.rb', line 52

def encrypt(*attributes)        
	include ActiveCrypto::Encrypted
	before_save :encrypt_attributes
	after_save :decrypt_attributes
  options=attributes.last.is_a?(Hash) ? attributes.pop : {}
  keyholder
  if options and options[:key]
module_eval <<-"end;"				 
	def session_key
		(send :#{options[:key]} ).send :session_key
	end	 
	@@external_key=true
end;
  end

  base64_encode = (options and options[:base64])
  module_eval <<-"end;"
    def self.ezcrypto_base64?
      #{base64_encode.to_s}
    end
  end;
  
  self.encrypted_attributes=attributes
end

#keyholderObject

Creates support in this class for holding a key. Adds the following methods:

  • enter_password(password,salt=“onetwothree”)

  • set_session_key(key)

  • session_key

Use it as follows:

class User < ActiveRecord::Base
	has_many :documents
	keyholder
end


92
93
94
95
# File 'lib/active_crypto.rb', line 92

def keyholder()
	include ActiveCrypto::AssociationKeyHolder   
	after_create :save_session_key       
end

#session_keysObject

:nodoc:



113
114
115
# File 'lib/active_crypto.rb', line 113

def session_keys() #:nodoc:
  @@session_keys
end

#session_keys=(keys) ⇒ Object

Sets the session_keys array. Only use these if you need to do something out of the ordinary, as it is handled



109
110
111
# File 'lib/active_crypto.rb', line 109

def session_keys=(keys) #:nodoc:
  @@session_keys=keys
end