lib/active_crypto.rb in ezcrypto-0.7 vs lib/active_crypto.rb in ezcrypto-0.7.2

- old
+ new

@@ -36,16 +36,18 @@ 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: +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 + encrypt :title,:body,:key=>:user, :base64 => true end =end def encrypt(*attributes) include ActiveCrypto::Encrypted @@ -59,11 +61,19 @@ (send :#{options[:key]} ).send :session_key end @@external_key=true end; end - self.encrypted_attributes=attributes + + base64_encode = (options and options[:base64]) + module_eval <<-"end;" + def self.ezcrypto_base64? + #{base64_encode.to_s} + end + end; + + self.encrypted_attributes=attributes end =begin rdoc Creates support in this class for holding a key. Adds the following methods: @@ -234,11 +244,11 @@ def _decrypt(data) if session_key.nil? raise MissingKeyError else if data - session_key.decrypt(data) + self.class.ezcrypto_base64? ? session_key.decrypt64(data) : session_key.decrypt(data) else nil end end end @@ -246,11 +256,11 @@ def _encrypt(data) if session_key.nil? raise MissingKeyError else if data - session_key.encrypt(data) + self.class.ezcrypto_base64? ? session_key.encrypt64(data) : session_key.encrypt(data) else nil end end end @@ -308,6 +318,8 @@ class MissingKeyError < RuntimeError end end ActiveRecord::Base.send :include, ActiveCrypto +require 'actionpack' +require 'action_controller' ActionController::Base.send :include, ActiveCrypto::ActionController