Module: ActiveCrypto::ClassMethods

Defined in:
lib/gems/ezcrypto-0.7/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.



91
92
93
# File 'lib/gems/ezcrypto-0.7/lib/active_crypto.rb', line 91

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 Include optional option :key, to specify an external KeyHolder, which holds the key used for encrypting and decrypting:

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


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gems/ezcrypto-0.7/lib/active_crypto.rb', line 50

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
  			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


82
83
84
85
# File 'lib/gems/ezcrypto-0.7/lib/active_crypto.rb', line 82

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

#session_keysObject

:nodoc:



103
104
105
# File 'lib/gems/ezcrypto-0.7/lib/active_crypto.rb', line 103

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



99
100
101
# File 'lib/gems/ezcrypto-0.7/lib/active_crypto.rb', line 99

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