ezcrypto 0.3 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ 0.5 July 19th, 2006 Good citizen release
2
+
3
+ I have cleaned up the ActiveCrypto namespaces. It now does not use ActiveRecord::Crypto, but ActiveCrypto::*, if you have called stuff directly please update your code.
4
+
5
+ The reason for this is to really be a good citizen in the world of rails and not unnecessarily pollute the ActiveRecord name space.
6
+
7
+ I have also created much more thorough unit tests and refactored some things that did not work in version 0.4 that the world thankfully did not see.
8
+
9
+ 0.4 Flawed internal release
10
+
1
11
  0.3 February 25th, 2006 new encrypted file support by Dirk (dirk.barnikel@gmx.de) Thanks Dirk.
2
12
 
3
13
  * Added test case for the file-related stuff. file-stuff
data/README CHANGED
@@ -1,152 +1,152 @@
1
- = EzCrypto - Easy to use Crypto for Ruby
2
-
3
- EzCrypto is an easy to use wrapper around the poorly documented OpenSSL ruby library.
4
-
5
- == Features
6
-
7
- * Defaults to AES 128 CBC
8
- * Will use the systems OpenSSL library for transparent hardware crypto support
9
- * Single class object oriented access to most commonly used features
10
- * Ruby like
11
-
12
- == Installation
13
-
14
- Download it from here:
15
-
16
- http://rubyforge.org/frs/?group_id=755&release_id=3321
17
-
18
- or install it via Ruby Gems:
19
-
20
- gem install ezruby
21
-
22
- == Simple examples
23
-
24
- ==== To encrypt:
25
-
26
- Generate a key using a password and a salt. Use the keys encrypt method to encrypt a strings worth of data:
27
-
28
- @key=EzCrypto::Key.with_password "password", "system salt"
29
- @encrypted=@key.encrypt "Top secret should not be revealed"
30
-
31
- ==== To decrypt:
32
-
33
- Same procedure as encrypt. Generate a key using a password and a salt. Use the keys decrypt method to decrypt a strings worth of data:
34
-
35
- @key=EzCrypto::Key.with_password "password", "system salt"
36
- @key.decrypt @encrypted
37
-
38
- ==== One liners:
39
-
40
- These simple examples use one line each:
41
-
42
- @encrypted=EzCrypto::Key.encrypt_with_password "password", @salt,"Top secret should not be revealed"
43
-
44
- EzCrypto::Key.decrypt_with_password "password", @salt,@encrypted
45
-
46
- == Keys
47
-
48
- The only class you need to know for most uses og EzCrypto is the Key class. You don't need understand ciphers or the encryption life cycle.
49
-
50
- ==== Generating a random key
51
-
52
- The most secure type of key is the randomly generated key:
53
-
54
- @key=EzCrypto::Key.generate
55
-
56
- ==== Initializing a key with raw key data
57
-
58
- If you already have a key from some other source, you simply have to call the constructor with the raw data:
59
-
60
- @key=EzCrypto::Key.new @binarykey
61
-
62
- ==== Initializing a Key with a Base64 encoded key
63
-
64
- As seen above you can create a key from a password. This should be used if you don't want the key to be stored on disk for example:
65
-
66
- @key=EzCrypto::Key.with_password "Secret password"
67
-
68
- ==== Initializing a Key with a Base64 encoded key
69
-
70
- If you already have a key from some other source in the popular Base64 encoded format, you use the decode class method:
71
-
72
- @key=EzCrypto::Key.decode @binarykey
73
-
74
- ==== Exporting the key
75
-
76
- To export or save a key use the encode method (or to_s) method for a Base64 encoded key or raw as the raw binary data.
77
-
78
- puts @key.encode
79
- puts @key.raw
80
-
81
- The raw method could be used for storing in a database using a tinyblob column.
82
-
83
- == Encryption and Decryption
84
-
85
- EzCrypto is optimized for simple encryption and decryption of strings. There are encrypt/decrypt pairs for normal binary use as well as for Base64 encoded use.
86
-
87
- ==== Regular raw use
88
-
89
- Assuming you have generated a key using one of the above methods:
90
-
91
- @encrypted=@key.encrypt("clear text")
92
- @decrypted=@key.decrypt(@encrypted)
93
- assert "clear text", @decrypted
94
-
95
- ==== Base64 encoded use
96
-
97
- This uses the encrypt64 and decrypt64 methods. Otherwise it is all the same:
98
-
99
- @encrypted=@key.encrypt64("clear text")
100
- @decrypted=@key.decrypt64(@encrypted)
101
- assert "clear text", @decrypted
102
-
103
- == FAQ
104
-
105
- === What algorithm does this use?
106
-
107
- It uses as the default algorithm the AES 128 bit standard. This is a very fast and highly secure algorithm specified as the national standard in the US. For more information see:
108
-
109
- http://en.wikipedia.org/wiki/AES
110
-
111
- === Only 128 bits. Is that enough?
112
-
113
- While it might sound like more would make it more secure, there is really no real security advantage for most commercial applications to use more than 128 bit AES.
114
-
115
- === What is Base64 encoding?
116
-
117
- This is the most efficient and commonly used encoding scheme for binary data. This is used amongst other things for email attachments. It is also very common to use it for encrypted data.
118
-
119
- === What is a Salt?
120
-
121
- A salt is just a piece of data we hash in with the password to create the key. If it is a server based application you could use store a salt within your source file. The salt must be the same for both encryption and decryption.
122
-
123
-
124
- == License
125
-
126
- EzCrypto and ActionCrypto is released under the MIT license.
127
-
128
-
129
- == Support
130
-
131
- To contact the author, send mail to pelleb@gmail.com
132
-
133
- Also see my blogs at:
134
- http://stakeventures.com and
135
- http://neubia.com
136
-
137
- This project was based on code used in my projects StakeItOut, WideWord and WideBlog.
138
-
139
- StakeItOut lets you securely share web services with your partners.
140
-
141
- https://stakeitout.com
142
-
143
- WideWord lets you collaboratively write and share documents that remain 100% encrypted on the server. Only you have the keys:
144
-
145
- http://wideword.net
146
-
147
- WideBlog is a secure private blogging system designed for private project blogs. It uses the same encryption technology as WideWord and is very easy to use:
148
-
149
- http://wideblog.net
150
-
151
-
152
- (C) 2005 Pelle Braendgaard
1
+ = EzCrypto - Easy to use Crypto for Ruby
2
+
3
+ EzCrypto is an easy to use wrapper around the poorly documented OpenSSL ruby library.
4
+
5
+ == Features
6
+
7
+ * Defaults to AES 128 CBC
8
+ * Will use the systems OpenSSL library for transparent hardware crypto support
9
+ * Single class object oriented access to most commonly used features
10
+ * Ruby like
11
+
12
+ == Installation
13
+
14
+ Download it from here:
15
+
16
+ http://rubyforge.org/frs/?group_id=755&release_id=3321
17
+
18
+ or install it via Ruby Gems:
19
+
20
+ gem install ezruby
21
+
22
+ == Simple examples
23
+
24
+ ==== To encrypt:
25
+
26
+ Generate a key using a password and a salt. Use the keys encrypt method to encrypt a strings worth of data:
27
+
28
+ @key=EzCrypto::Key.with_password "password", "system salt"
29
+ @encrypted=@key.encrypt "Top secret should not be revealed"
30
+
31
+ ==== To decrypt:
32
+
33
+ Same procedure as encrypt. Generate a key using a password and a salt. Use the keys decrypt method to decrypt a strings worth of data:
34
+
35
+ @key=EzCrypto::Key.with_password "password", "system salt"
36
+ @key.decrypt @encrypted
37
+
38
+ ==== One liners:
39
+
40
+ These simple examples use one line each:
41
+
42
+ @encrypted=EzCrypto::Key.encrypt_with_password "password", @salt,"Top secret should not be revealed"
43
+
44
+ EzCrypto::Key.decrypt_with_password "password", @salt,@encrypted
45
+
46
+ == Keys
47
+
48
+ The only class you need to know for most uses og EzCrypto is the Key class. You don't need understand ciphers or the encryption life cycle.
49
+
50
+ ==== Generating a random key
51
+
52
+ The most secure type of key is the randomly generated key:
53
+
54
+ @key=EzCrypto::Key.generate
55
+
56
+ ==== Initializing a key with raw key data
57
+
58
+ If you already have a key from some other source, you simply have to call the constructor with the raw data:
59
+
60
+ @key=EzCrypto::Key.new @binarykey
61
+
62
+ ==== Initializing a Key with a Base64 encoded key
63
+
64
+ As seen above you can create a key from a password. This should be used if you don't want the key to be stored on disk for example:
65
+
66
+ @key=EzCrypto::Key.with_password "Secret password"
67
+
68
+ ==== Initializing a Key with a Base64 encoded key
69
+
70
+ If you already have a key from some other source in the popular Base64 encoded format, you use the decode class method:
71
+
72
+ @key=EzCrypto::Key.decode @binarykey
73
+
74
+ ==== Exporting the key
75
+
76
+ To export or save a key use the encode method (or to_s) method for a Base64 encoded key or raw as the raw binary data.
77
+
78
+ puts @key.encode
79
+ puts @key.raw
80
+
81
+ The raw method could be used for storing in a database using a tinyblob column.
82
+
83
+ == Encryption and Decryption
84
+
85
+ EzCrypto is optimized for simple encryption and decryption of strings. There are encrypt/decrypt pairs for normal binary use as well as for Base64 encoded use.
86
+
87
+ ==== Regular raw use
88
+
89
+ Assuming you have generated a key using one of the above methods:
90
+
91
+ @encrypted=@key.encrypt("clear text")
92
+ @decrypted=@key.decrypt(@encrypted)
93
+ assert "clear text", @decrypted
94
+
95
+ ==== Base64 encoded use
96
+
97
+ This uses the encrypt64 and decrypt64 methods. Otherwise it is all the same:
98
+
99
+ @encrypted=@key.encrypt64("clear text")
100
+ @decrypted=@key.decrypt64(@encrypted)
101
+ assert "clear text", @decrypted
102
+
103
+ == FAQ
104
+
105
+ === What algorithm does this use?
106
+
107
+ It uses as the default algorithm the AES 128 bit standard. This is a very fast and highly secure algorithm specified as the national standard in the US. For more information see:
108
+
109
+ http://en.wikipedia.org/wiki/AES
110
+
111
+ === Only 128 bits. Is that enough?
112
+
113
+ While it might sound like more would make it more secure, there is really no real security advantage for most commercial applications to use more than 128 bit AES.
114
+
115
+ === What is Base64 encoding?
116
+
117
+ This is the most efficient and commonly used encoding scheme for binary data. This is used amongst other things for email attachments. It is also very common to use it for encrypted data.
118
+
119
+ === What is a Salt?
120
+
121
+ A salt is just a piece of data we hash in with the password to create the key. If it is a server based application you could use store a salt within your source file. The salt must be the same for both encryption and decryption.
122
+
123
+
124
+ == License
125
+
126
+ EzCrypto and ActionCrypto is released under the MIT license.
127
+
128
+
129
+ == Support
130
+
131
+ To contact the author, send mail to pelleb@gmail.com
132
+
133
+ Also see my blogs at:
134
+ http://stakeventures.com and
135
+ http://neubia.com
136
+
137
+ This project was based on code used in my projects StakeItOut, WideWord and WideBlog.
138
+
139
+ StakeItOut lets you securely share web services with your partners.
140
+
141
+ https://stakeitout.com
142
+
143
+ WideWord lets you collaboratively write and share documents that remain 100% encrypted on the server. Only you have the keys:
144
+
145
+ http://wideword.net
146
+
147
+ WideBlog is a secure private blogging system designed for private project blogs. It uses the same encryption technology as WideWord and is very easy to use:
148
+
149
+ http://wideblog.net
150
+
151
+
152
+ (C) 2005 Pelle Braendgaard
data/init.rb CHANGED
@@ -1,2 +1 @@
1
1
  require 'active_crypto.rb'
2
- #ActiveRecord::Base.send :include, ActiveCrypto
@@ -1,294 +1,313 @@
1
- require "ezcrypto.rb"
2
- module ActiveRecord # :nodoc:
3
- module Crypto #:nodoc:
4
-
5
- def self.append_features(base) #:nodoc:
6
- super
7
- base.extend(ClassMethods)
8
- end
9
-
10
- =begin rdoc
11
-
12
- Usage is very simple. You will generally only need the two class methods listed here in your ActiveRecord class model.
13
-
14
- == License
15
-
16
- ActiveCrypto and EzCrypto are released under the MIT license.
17
-
18
-
19
- == Support
20
-
21
- To contact the author, send mail to pelleb@gmail.com
22
-
23
- Also see my blogs at:
24
- http://stakeventures.com and
25
- http://neubia.com
26
-
27
- This project was based on code used in my project StakeItOut, where you can securely share web services with your partners.
28
- https://stakeitout.com
29
-
30
- (C) 2005 Pelle Braendgaard
31
-
32
- =end
33
- module ClassMethods
34
- @@session_keys={}
35
-
36
- =begin rdoc
37
- Turn encryption on for this record. List all encrypted attributes
38
-
39
- class Document < ActiveRecord::Base
40
- encrypt :title,:body
41
- end
42
-
43
- Include optional option :key, to specify an external KeyHolder, which holds the key used for encrypting and decrypting:
44
-
45
- class Document < ActiveRecord::Base
46
- belongs_to :user
47
- encrypt :title,:body,:key=>:user
48
- end
49
-
50
- =end
51
- def encrypt(*attributes)
52
- include ActiveRecord::Crypto::Encrypted
53
- alias_method :orig_write_attribute, :write_attribute
54
- alias_method :write_attribute,:write_encrypted_attribute
55
- options=attributes.last.is_a?(Hash) ? attributes.pop : {}
56
- if options and options[:key]
57
- include ActiveRecord::Crypto::AssociationKeyHolder
58
-
59
- module_eval <<-"end;"
60
- def session_key
61
- (send :#{options[:key]} ).send :session_key
62
- end
63
- @@external_key=true
64
- end;
65
- else
66
- include ActiveRecord::Crypto::KeyHolder
67
- end
68
-
69
- self.encrypted_attributes=attributes
70
- for enc in attributes
71
-
72
- module_eval <<-"end;"
73
- def #{enc.to_s}
74
- _decrypt(read_attribute("#{enc.to_s}"))
75
- end
76
- end;
77
- end
78
- end
79
-
80
- =begin rdoc
81
- Creates support in this class for holding a key. Adds the following methods:
82
-
83
- * enter_password(password,salt="onetwothree")
84
- * set_session_key(key)
85
- * session_key
86
-
87
- Use it as follows:
88
-
89
- class User < ActiveRecord::Base
90
- has_many :documents
91
- keyholder
92
- end
93
-
94
- =end
95
- def keyholder()
96
- include ActiveRecord::Crypto::AssociationKeyHolder
97
- end
98
-
99
- =begin rdoc
100
- Clears the session_key array. Generally this is handled automatically as a filter in ActionController. Only use these if you need to
101
- do something out of the ordinary.
102
- =end
103
- def clear_session_keys() #:nodoc:
104
- @@session_keys.clear
105
- end
106
-
107
- =begin rdoc
108
- Sets the session_keys array. Only use these if you need to
109
- do something out of the ordinary, as it is handled
110
- =end
111
- def session_keys=(keys) #:nodoc:
112
- @@session_keys=keys
113
- end
114
-
115
- def session_keys() #:nodoc:
116
- @@session_keys
117
- end
118
- end
119
-
120
- =begin rdoc
121
- This module handles all standard key management features.
122
- =end
123
- module KeyHolder
124
-
125
- =begin rdoc
126
- Creates a key for object based on given password and an optional salt.
127
- =end
128
- def enter_password(password,salt="onetwothree")
129
- set_session_key(EzCrypto::Key.with_password(password, salt))
130
- end
131
-
132
- =begin rdoc
133
- Decodes the Base64 encoded key and uses it as it's session key
134
- =end
135
- def set_encoded_key(enc)
136
- set_session_key(EzCrypto::Key.decode(enc))
137
- end
138
- =begin rdoc
139
- Sets a session key for the object. This should be a EzCrypto::Key instance.
140
- =end
141
- def set_session_key(key)
142
- @session_key=key
143
- end
144
-
145
- =begin rdoc
146
- Returns the session_key
147
- =end
148
- def session_key
149
- @session_key
150
- end
151
-
152
- end
153
-
154
- module AssociationKeyHolder
155
- include KeyHolder
156
- =begin rdoc
157
- Sets a session key for the object. This should be a EzCrypto::Key instance.
158
- =end
159
- def set_session_key(key)
160
- Base.session_keys[session_key_id]=key
161
- end
162
-
163
- =begin rdoc
164
- Returns the session_key
165
- =end
166
- def session_key
167
- if session_key_id
168
- Base.session_keys[session_key_id]
169
- else
170
- nil
171
- end
172
- end
173
-
174
- private
175
-
176
- def session_key_id
177
- "#{self.class.to_s}:#{id}"
178
- end
179
- end
180
-
181
- module Encrypted #:nodoc:
182
- def self.append_features(base) #:nodoc:
183
- super
184
- base.module_eval <<-"end;"
185
- @@encrypted_attributes=[]
186
- def encrypted_attributes
187
- @@encrypted_attributes
188
- end
189
-
190
- def #{base.to_s}.encrypted_attributes=(attrs)
191
- @@encrypted_attributes=attrs
192
- end
193
- end;
194
- end
195
-
196
- def write_encrypted_attribute(name,value)
197
- if encrypted_attributes.include?(name.to_sym)
198
- orig_write_attribute(name,_encrypt(value))
199
- else
200
- orig_write_attribute(name,value)
201
- end
202
- end
203
- end
204
-
205
- private
206
-
207
- def _decrypt(data)
208
- if session_key.nil?
209
- raise MissingKeyError
210
- else
211
- if data
212
- session_key.decrypt(data)
213
- else
214
- nil
215
- end
216
- end
217
- end
218
-
219
- def _encrypt(data)
220
- if session_key.nil?
221
- raise MissingKeyError
222
- else
223
- if data
224
- session_key.encrypt(data)
225
- else
226
- nil
227
- end
228
- end
229
- end
230
-
231
- end
232
-
233
- class Base # :nodoc:
234
- include ActiveRecord::Crypto
235
- end
236
- end
237
-
238
- module ActionController # :nodoc:
239
- =begin rdoc
240
- This includes some basic support in the ActionController for handling session keys. It creates two filters one before the action and one after.
241
- These do the following:
242
-
243
- If the users session already has a 'session_keys' value it loads it into the ActiveRecord::Base.session_keys class field. If not it
244
- clears any existing session_keys.
245
-
246
- Leaving the action it stores any session_keys in the corresponding session variable.
247
-
248
- These filters are automatically enabled. You do not have to do anything.
249
-
250
- To manually clear the session keys call clear_session_keys. This should be done for example as part of a session log off action.
251
- =end
252
- module CryptoSupport
253
-
254
- def self.append_features(base) #:nodoc:
255
- super
256
- base.send :prepend_before_filter, :load_session_keys
257
- base.send :prepend_after_filter, :save_session_keys
258
- end
259
-
260
- =begin rdoc
261
- Clears the session keys. Call this when a user logs of.
262
- =end
263
- def clear_session_keys
264
- ActiveRecord::Base.clear_session_keys
265
- end
266
-
267
-
268
- private
269
- def load_session_keys
270
- if @session['session_keys']
271
- ActiveRecord::Base.session_keys=@session['session_keys']
272
- else
273
- ActiveRecord::Base.clear_session_keys
274
- end
275
- end
276
-
277
- def save_session_keys
278
- if ActiveRecord::Base.session_keys.size>0
279
- @session['session_keys']=ActiveRecord::Base.session_keys
280
- else
281
- @session['session_keys']=nil
282
- end
283
- end
284
-
285
- end
286
-
287
- class Base # :nodoc:
288
- include CryptoSupport
289
- end
290
-
291
- end
292
-
293
- class MissingKeyError < RuntimeError
294
- end
1
+ require "ezcrypto.rb"
2
+ module ActiveCrypto # :nodoc:
3
+
4
+ def self.append_features(base) #:nodoc:
5
+ super
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ =begin rdoc
10
+
11
+ Usage is very simple. You will generally only need the two class methods listed here in your ActiveRecord class model.
12
+
13
+ == License
14
+
15
+ ActiveCrypto and EzCrypto are released under the MIT license.
16
+
17
+
18
+ == Support
19
+
20
+ To contact the author, send mail to pelleb@gmail.com
21
+
22
+ Also see my blogs at:
23
+ http://stakeventures.com and
24
+ http://neubia.com
25
+
26
+ This project was based on code used in my project StakeItOut, where you can securely share web services with your partners.
27
+ https://stakeitout.com
28
+
29
+ (C) 2005 Pelle Braendgaard
30
+
31
+ =end
32
+ module ClassMethods
33
+ @@session_keys={}
34
+
35
+ =begin rdoc
36
+ Turn encryption on for this record. List all encrypted attributes
37
+
38
+ class Document < ActiveRecord::Base
39
+ encrypt :title,:body
40
+ end
41
+
42
+ Include optional option :key, to specify an external KeyHolder, which holds the key used for encrypting and decrypting:
43
+
44
+ class Document < ActiveRecord::Base
45
+ belongs_to :user
46
+ encrypt :title,:body,:key=>:user
47
+ end
48
+
49
+ =end
50
+ def encrypt(*attributes)
51
+ include ActiveCrypto::Encrypted
52
+ before_save :encrypt_attributes
53
+ after_save :decrypt_attributes
54
+ options=attributes.last.is_a?(Hash) ? attributes.pop : {}
55
+ keyholder
56
+ if options and options[:key]
57
+ module_eval <<-"end;"
58
+ def session_key
59
+ (send :#{options[:key]} ).send :session_key
60
+ end
61
+ @@external_key=true
62
+ end;
63
+ end
64
+ self.encrypted_attributes=attributes
65
+ end
66
+
67
+ =begin rdoc
68
+ Creates support in this class for holding a key. Adds the following methods:
69
+
70
+ * enter_password(password,salt="onetwothree")
71
+ * set_session_key(key)
72
+ * session_key
73
+
74
+ Use it as follows:
75
+
76
+ class User < ActiveRecord::Base
77
+ has_many :documents
78
+ keyholder
79
+ end
80
+
81
+ =end
82
+ def keyholder()
83
+ include ActiveCrypto::AssociationKeyHolder
84
+ after_create :save_session_key
85
+ end
86
+
87
+ =begin rdoc
88
+ Clears the session_key array. Generally this is handled automatically as a filter in ActionController. Only use these if you need to
89
+ do something out of the ordinary.
90
+ =end
91
+ def clear_session_keys() #:nodoc:
92
+ @@session_keys.clear
93
+ end
94
+
95
+ =begin rdoc
96
+ Sets the session_keys array. Only use these if you need to
97
+ do something out of the ordinary, as it is handled
98
+ =end
99
+ def session_keys=(keys) #:nodoc:
100
+ @@session_keys=keys
101
+ end
102
+
103
+ def session_keys() #:nodoc:
104
+ @@session_keys
105
+ end
106
+
107
+ end
108
+
109
+ =begin rdoc
110
+ This module handles all standard key management features.
111
+ =end
112
+ module KeyHolder
113
+
114
+ =begin rdoc
115
+ Creates a key for object based on given password and an optional salt.
116
+ =end
117
+ def enter_password(password,salt="onetwothree")
118
+ set_session_key(EzCrypto::Key.with_password(password, salt))
119
+ end
120
+
121
+ =begin rdoc
122
+ Decodes the Base64 encoded key and uses it as it's session key
123
+ =end
124
+ def set_encoded_key(enc)
125
+ set_session_key(EzCrypto::Key.decode(enc))
126
+ end
127
+ =begin rdoc
128
+ Sets a session key for the object. This should be a EzCrypto::Key instance.
129
+ =end
130
+ def set_session_key(key)
131
+ @session_key=key
132
+ self.decrypt_attributes if self.class.include? Encrypted
133
+ end
134
+
135
+ =begin rdoc
136
+ Returns the session_key
137
+ =end
138
+ def session_key
139
+ @session_key
140
+ end
141
+
142
+ end
143
+
144
+ module AssociationKeyHolder
145
+ include ActiveCrypto::KeyHolder
146
+
147
+
148
+ def save_session_key
149
+ ActiveRecord::Base.session_keys[session_key_id]=@session_key if @session_key
150
+ end
151
+ =begin rdoc
152
+ Sets a session key for the object. This should be a EzCrypto::Key instance.
153
+ =end
154
+ def set_session_key(key)
155
+ if self.new_record?
156
+ @session_key=key
157
+ else
158
+ ActiveRecord::Base.session_keys[session_key_id]=key
159
+ end
160
+ decrypt_attributes if self.class.include? Encrypted #if respond_to?(:decrypt_attributes)
161
+
162
+ end
163
+
164
+ =begin rdoc
165
+ Returns the session_key
166
+ =end
167
+ def session_key
168
+ if self.new_record?
169
+ @session_key
170
+ else
171
+ ActiveRecord::Base.session_keys[session_key_id]
172
+ end
173
+ end
174
+
175
+
176
+
177
+ def session_key_id
178
+ "#{self.class.to_s}:#{id}"
179
+ end
180
+
181
+ end
182
+
183
+ module Encrypted #:nodoc:
184
+ def self.append_features(base) #:nodoc:
185
+ super
186
+ base.extend ClassAccessors
187
+ end
188
+
189
+ module ClassAccessors
190
+ def encrypted_attributes
191
+ @encrypted_attributes||=[]
192
+ end
193
+
194
+ def encrypted_attributes=(attrs)
195
+ @encrypted_attributes=attrs
196
+ end
197
+
198
+ end
199
+
200
+ protected
201
+
202
+ def encrypt_attributes
203
+ if !is_encrypted?
204
+ self.class.encrypted_attributes.each do |key|
205
+ value=read_attribute(key)
206
+ write_attribute(key,_encrypt(value)) if value
207
+ end
208
+ @is_encrypted=true
209
+ end
210
+ true
211
+ end
212
+
213
+ def decrypt_attributes
214
+ if is_encrypted?
215
+ self.class.encrypted_attributes.each do |key|
216
+ value=read_attribute(key)
217
+ write_attribute(key,_decrypt(value)) if value
218
+ end
219
+ @is_encrypted=false
220
+ end
221
+ true
222
+ end
223
+
224
+ def after_find
225
+ @is_encrypted=true
226
+ decrypt_attributes unless session_key.nil?
227
+ end
228
+
229
+ private
230
+ def is_encrypted?
231
+ @is_encrypted
232
+ end
233
+
234
+ def _decrypt(data)
235
+ if session_key.nil?
236
+ raise MissingKeyError
237
+ else
238
+ if data
239
+ session_key.decrypt(data)
240
+ else
241
+ nil
242
+ end
243
+ end
244
+ end
245
+
246
+ def _encrypt(data)
247
+ if session_key.nil?
248
+ raise MissingKeyError
249
+ else
250
+ if data
251
+ session_key.encrypt(data)
252
+ else
253
+ nil
254
+ end
255
+ end
256
+ end
257
+
258
+ end
259
+
260
+
261
+ module ActionController # :nodoc:
262
+ =begin rdoc
263
+ This includes some basic support in the ActionController for handling session keys. It creates two filters one before the action and one after.
264
+ These do the following:
265
+
266
+ If the users session already has a 'session_keys' value it loads it into the ActiveRecord::Base.session_keys class field. If not it
267
+ clears any existing session_keys.
268
+
269
+ Leaving the action it stores any session_keys in the corresponding session variable.
270
+
271
+ These filters are automatically enabled. You do not have to do anything.
272
+
273
+ To manually clear the session keys call clear_session_keys. This should be done for example as part of a session log off action.
274
+ =end
275
+ def self.append_features(base) #:nodoc:
276
+ super
277
+ base.send :prepend_before_filter, :load_session_keys
278
+ base.send :prepend_after_filter, :save_session_keys
279
+ end
280
+
281
+ =begin rdoc
282
+ Clears the session keys. Call this when a user logs of.
283
+ =end
284
+ def clear_session_keys
285
+ ActiveRecord::Base.clear_session_keys
286
+ end
287
+
288
+
289
+ private
290
+ def load_session_keys
291
+ if session['session_keys']
292
+ ActiveRecord::Base.session_keys=session['session_keys']
293
+ else
294
+ ActiveRecord::Base.clear_session_keys
295
+ end
296
+ end
297
+
298
+ def save_session_keys
299
+ if ActiveRecord::Base.session_keys.size>0
300
+ session['session_keys']=ActiveRecord::Base.session_keys
301
+ else
302
+ session['session_keys']=nil
303
+ end
304
+ end
305
+
306
+
307
+ end
308
+
309
+ class MissingKeyError < RuntimeError
310
+ end
311
+ end
312
+ ActiveRecord::Base.send :include, ActiveCrypto
313
+ ActionController::Base.send :include, ActiveCrypto::ActionController