ezcrypto 0.3 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +10 -0
- data/README +152 -152
- data/init.rb +0 -1
- data/lib/active_crypto.rb +313 -294
- data/lib/ezcrypto.rb +24 -5
- data/rakefile +8 -7
- data/test/active_crypto_test.rb +22 -8
- data/test/association_key_holder_test.rb +38 -0
- data/test/debug.log +203 -0
- data/test/encrypt_test.rb +37 -0
- data/test/key_holder_test.rb +44 -0
- metadata +31 -22
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
data/lib/active_crypto.rb
CHANGED
@@ -1,294 +1,313 @@
|
|
1
|
-
require "ezcrypto.rb"
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
http://
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
if options and options[:key]
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
=
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
def
|
104
|
-
@@session_keys
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
=
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
def
|
149
|
-
@session_key
|
150
|
-
end
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
=
|
157
|
-
|
158
|
-
=
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
end
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
=
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
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
|