lib/active_crypto.rb in ezcrypto-0.2.1 vs lib/active_crypto.rb in ezcrypto-0.3
- old
+ new
@@ -1,6 +1,6 @@
-require "ezCrypto"
+require "ezcrypto.rb"
module ActiveRecord # :nodoc:
module Crypto #:nodoc:
def self.append_features(base) #:nodoc:
super
@@ -47,22 +47,27 @@
encrypt :title,:body,:key=>:user
end
=end
def encrypt(*attributes)
- include ActiveRecord::Crypto::Encrypted
- alias_method :orig_write_attribute, :write_attribute
- alias_method :write_attribute,:write_encrypted_attribute
- options=attributes.last.is_a?(Hash) ? attributes.pop : {}
- if options and options[:key]
- module_eval <<-"end;"
- def session_key
- (send :#{options[:key]} ).send :session_key
- end
- end;
-
- end
+ include ActiveRecord::Crypto::Encrypted
+ alias_method :orig_write_attribute, :write_attribute
+ alias_method :write_attribute,:write_encrypted_attribute
+ options=attributes.last.is_a?(Hash) ? attributes.pop : {}
+ if options and options[:key]
+ include ActiveRecord::Crypto::AssociationKeyHolder
+
+ module_eval <<-"end;"
+ def session_key
+ (send :#{options[:key]} ).send :session_key
+ end
+ @@external_key=true
+ end;
+ else
+ include ActiveRecord::Crypto::KeyHolder
+ end
+
self.encrypted_attributes=attributes
for enc in attributes
module_eval <<-"end;"
def #{enc.to_s}
@@ -86,11 +91,11 @@
keyholder
end
=end
def keyholder()
- include ActiveRecord::Crypto::KeyHolder
+ include ActiveRecord::Crypto::AssociationKeyHolder
end
=begin rdoc
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.
@@ -119,11 +124,11 @@
=begin rdoc
Creates a key for object based on given password and an optional salt.
=end
def enter_password(password,salt="onetwothree")
- set_session_key(EzCrypto::Key.with_password password, salt)
+ set_session_key(EzCrypto::Key.with_password(password, salt))
end
=begin rdoc
Decodes the Base64 encoded key and uses it as it's session key
=end
@@ -132,29 +137,50 @@
end
=begin rdoc
Sets a session key for the object. This should be a EzCrypto::Key instance.
=end
def set_session_key(key)
- Base.session_keys[session_key_id]=key
+ @session_key=key
end
=begin rdoc
Returns the session_key
=end
def session_key
- Base.session_keys[session_key_id]
+ @session_key
end
+ end
+
+ module AssociationKeyHolder
+ include KeyHolder
+=begin rdoc
+Sets a session key for the object. This should be a EzCrypto::Key instance.
+=end
+ def set_session_key(key)
+ Base.session_keys[session_key_id]=key
+ end
+
+=begin rdoc
+Returns the session_key
+=end
+ def session_key
+ if session_key_id
+ Base.session_keys[session_key_id]
+ else
+ nil
+ end
+ end
+
private
-
+
def session_key_id
"#{self.class.to_s}:#{id}"
end
end
module Encrypted #:nodoc:
- include ActiveRecord::Crypto::KeyHolder
def self.append_features(base) #:nodoc:
super
base.module_eval <<-"end;"
@@encrypted_attributes=[]
def encrypted_attributes
@@ -180,18 +206,26 @@
def _decrypt(data)
if session_key.nil?
raise MissingKeyError
else
- session_key.decrypt(data)
+ if data
+ session_key.decrypt(data)
+ else
+ nil
+ end
end
end
def _encrypt(data)
if session_key.nil?
raise MissingKeyError
else
- session_key.encrypt(data)
+ if data
+ session_key.encrypt(data)
+ else
+ nil
+ end
end
end
end