ezcrypto 0.1.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,10 @@
1
+ 0.2 October 30th, 2005 Ruby on Rails integration
2
+
3
+ As promised I have now included my first version of ActiveCrypto the crypto layer for ActiveRecord and Ruby on Rails.
4
+
5
+ 0.1.1 August 27, 2005 Minor fixes
6
+
7
+ Thanks to Jason Vasquez mugatu at mugfu dot com for noticing that Key#to_s
8
+ called the nonexistent encoded method.
9
+
10
+ I also made a few slight changes to the documentation.
data/README CHANGED
@@ -113,7 +113,7 @@ A salt is just a piece of data we hash in with the password to create the key. I
113
113
 
114
114
  == License
115
115
 
116
- Action Web Service is released under the MIT license.
116
+ EzCrypto and ActionCrypto is released under the MIT license.
117
117
 
118
118
 
119
119
  == Support
@@ -0,0 +1,110 @@
1
+ = ActiveCrypto - Easy to use Crypto for Ruby on Rails
2
+
3
+ ActiveCrypto is based on EzCrypto and provides application oriented crypto support for Ruby on Rails applications.
4
+
5
+ == Features
6
+
7
+ * Transparent encryption/decryption
8
+ * Ruby on Rails like domain language
9
+
10
+ == Simple examples
11
+
12
+ ==== A simple encrypted class
13
+
14
+ You specify in your class which fields are encrypted:
15
+
16
+ class Document < ActiveRecord::Base
17
+ encrypt :title,:body
18
+ end
19
+
20
+ Two encrypt it you need to enter a key. For ease of use there is a method called enter_password which sets the key based on a password of your choice.
21
+
22
+ doc=Document.new
23
+ doc.enter_password "This stuff is secret man!!!"
24
+ doc.title="Plan to take over the world"
25
+ doc.body="Write apps in Rails"
26
+ doc.save
27
+
28
+ This needs to be done as well if you want to read your document:
29
+
30
+ doc=Document.find 1
31
+ doc.enter_password "This stuff is secret man!!!"
32
+ puts doc.name
33
+
34
+ If you don't remember to set a key it will through a MissingKeyError.
35
+
36
+ ==== More realistic example with KeyHolder
37
+
38
+ It probably isn't much use if each record needs its own key. The solution to this is the KeyHolder. A KeyHolder is an object that holds keys for use by other objects. A typical example would be a user.
39
+
40
+ class User < ActiveRecord::Base
41
+ has_many :documents
42
+ keyholder
43
+ end
44
+
45
+ We use standard ActiveRecord associations to associate the User with his documents. We also need to specify that he is a keyholder. We now modify our Document class as follows:
46
+
47
+ class Document < ActiveRecord::Base
48
+ belongs_to :user
49
+ encrypt :title,:body,:key=>:user
50
+ end
51
+
52
+ We have the standard associations going on here, but we have also added the option :key=>:user to the encrypt statement. Now we could do this:
53
+
54
+ @user=User.new
55
+ @user.enter_password "This stuff is secret man!!!"
56
+ @user.save
57
+
58
+ @doc=Document.new
59
+ @doc.user=@user
60
+ @doc.title="Plan to take over the world"
61
+ @doc.body="Write apps in Rails"
62
+ @doc.save
63
+
64
+ You could also do ordinary rails like stuf such as:
65
+
66
+ @user.documents.each do |doc|
67
+ puts doc.name
68
+ end
69
+
70
+ Decryption is done transparently.
71
+
72
+ When doing this within a rails application, active_crypto automatically maintains a list of keys for each user session. Besides the 2 steps below you don't need to do anything special within your controller.
73
+
74
+ 1. When a user logs on with a password enter his password like this:
75
+
76
+ @user.enter_password @params['password']
77
+
78
+ 2. When a user logs off call the following
79
+
80
+ clear_session_keys
81
+
82
+ == Usage as a Rails plugin
83
+
84
+ Just unpack it into your $MY_RAILS_PROJECTS/vendor/plugins folder to use it as a self contained plugin. Otherwise you can install it as a gem using:
85
+
86
+ $ gem install ezcrypto
87
+
88
+ Then make sure to require "active_crypto.rb" at the end of your environment.rb file.
89
+
90
+ == Database Schema issues
91
+
92
+ ActiveCrypto doesn't really care about the schema, but that said you do need a schema that will accept and not mangle it's output. On MySQL I normally use TINYBLOB instead of VARCHAR and BLOB instead of TEXT.
93
+
94
+ == License
95
+
96
+ EzCrypto and ActionCrypto is released under the MIT license.
97
+
98
+
99
+ == Support
100
+
101
+ To contact the author, send mail to pelleb@gmail.com
102
+
103
+ Also see my blogs at:
104
+ http://stakeventures.com and
105
+ http://neubia.com
106
+
107
+ This project was based on code used in my project StakeItOut, where you can securely share web services with your partners.
108
+ https://stakeitout.com
109
+
110
+ (C) 2005 Pelle Braendgaard
data/lib/CVS/Entries CHANGED
@@ -1 +1,3 @@
1
+ /active_crypto.rb/1.3/Sun Oct 30 22:41:10 2005//
2
+ /ezcrypto.rb/1.4/Sun Oct 30 22:24:34 2005//
1
3
  D
@@ -0,0 +1,254 @@
1
+ require "ezCrypto"
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
+ module_eval <<-"end;"
58
+ def session_key
59
+ (send :#{options[:key]} ).send :session_key
60
+ end
61
+ end;
62
+
63
+ end
64
+ self.encrypted_attributes=attributes
65
+ for enc in attributes
66
+
67
+ module_eval <<-"end;"
68
+ def #{enc.to_s}
69
+ _decrypt(read_attribute("#{enc.to_s}"))
70
+ end
71
+ end;
72
+ end
73
+ end
74
+
75
+ =begin rdoc
76
+ Creates support in this class for holding a key. Adds the following methods:
77
+
78
+ * enter_password(password,salt="onetwothree")
79
+ * set_session_key(key)
80
+ * session_key
81
+
82
+ Use it as follows:
83
+
84
+ class User < ActiveRecord::Base
85
+ has_many :documents
86
+ keyholder
87
+ end
88
+
89
+ =end
90
+ def keyholder()
91
+ include ActiveRecord::Crypto::KeyHolder
92
+ end
93
+
94
+ =begin rdoc
95
+ Clears the session_key array. Generally this is handled automatically as a filter in ActionController. Only use these if you need to
96
+ do something out of the ordinary.
97
+ =end
98
+ def clear_session_keys() #:nodoc:
99
+ @@session_keys.clear
100
+ end
101
+
102
+ =begin rdoc
103
+ Sets the session_keys array. Only use these if you need to
104
+ do something out of the ordinary, as it is handled
105
+ =end
106
+ def session_keys=(keys) #:nodoc:
107
+ @@session_keys=keys
108
+ end
109
+
110
+ def session_keys() #:nodoc:
111
+ @@session_keys
112
+ end
113
+ end
114
+
115
+ =begin rdoc
116
+ This module handles all standard key management features.
117
+ =end
118
+ module KeyHolder
119
+
120
+ =begin rdoc
121
+ Creates a key for object based on given password and an optional salt.
122
+ =end
123
+ def enter_password(password,salt="onetwothree")
124
+ set_session_key(EzCrypto::Key.with_password password, salt)
125
+ end
126
+
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
+ Base.session_keys[session_key_id]=key
132
+ end
133
+
134
+ =begin rdoc
135
+ Returns the session_key
136
+ =end
137
+ def session_key
138
+ Base.session_keys[session_key_id]
139
+ end
140
+
141
+ private
142
+
143
+ def session_key_id
144
+ "#{self.class.to_s}:#{id}"
145
+ end
146
+ end
147
+
148
+ module Encrypted #:nodoc:
149
+ include ActiveRecord::Crypto::KeyHolder
150
+ def self.append_features(base) #:nodoc:
151
+ super
152
+ base.module_eval <<-"end;"
153
+ @@encrypted_attributes=[]
154
+ def encrypted_attributes
155
+ @@encrypted_attributes
156
+ end
157
+
158
+ def #{base.to_s}.encrypted_attributes=(attrs)
159
+ @@encrypted_attributes=attrs
160
+ end
161
+ end;
162
+ end
163
+
164
+ def write_encrypted_attribute(name,value)
165
+ if encrypted_attributes.include?(name.to_sym)
166
+ orig_write_attribute(name,_encrypt(value))
167
+ else
168
+ orig_write_attribute(name,value)
169
+ end
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ def _decrypt(data)
176
+ if session_key.nil?
177
+ raise MissingKeyError
178
+ else
179
+ session_key.decrypt(data)
180
+ end
181
+ end
182
+
183
+ def _encrypt(data)
184
+ if session_key.nil?
185
+ raise MissingKeyError
186
+ else
187
+ session_key.encrypt(data)
188
+ end
189
+ end
190
+
191
+ end
192
+
193
+ class Base # :nodoc:
194
+ include ActiveRecord::Crypto
195
+ end
196
+ end
197
+
198
+ module ActionController # :nodoc:
199
+ =begin rdoc
200
+ This includes some basic support in the ActionController for handling session keys. It creates two filters one before the action and one after.
201
+ These do the following:
202
+
203
+ If the users session already has a 'session_keys' value it loads it into the ActiveRecord::Base.session_keys class field. If not it
204
+ clears any existing session_keys.
205
+
206
+ Leaving the action it stores any session_keys in the corresponding session variable.
207
+
208
+ These filters are automatically enabled. You do not have to do anything.
209
+
210
+ To manually clear the session keys call clear_session_keys. This should be done for example as part of a session log off action.
211
+ =end
212
+ module CryptoSupport
213
+
214
+ def self.append_features(base) #:nodoc:
215
+ super
216
+ base.send :prepend_before_filter, :load_session_keys
217
+ base.send :prepend_after_filter, :save_session_keys
218
+ end
219
+
220
+ =begin rdoc
221
+ Clears the session keys. Call this when a user logs of.
222
+ =end
223
+ def clear_session_keys
224
+ ActiveRecord::Base.clear_session_keys
225
+ end
226
+
227
+
228
+ private
229
+ def load_session_keys
230
+ if @session['session_keys']
231
+ ActiveRecord::Base.session_keys=@session['session_keys']
232
+ else
233
+ ActiveRecord::Base.clear_session_keys
234
+ end
235
+ end
236
+
237
+ def save_session_keys
238
+ if ActiveRecord::Base.session_keys.size>0
239
+ @session['session_keys']=ActiveRecord::Base.session_keys
240
+ else
241
+ @session['session_keys']=nil
242
+ end
243
+ end
244
+
245
+ end
246
+
247
+ class Base # :nodoc:
248
+ include CryptoSupport
249
+ end
250
+
251
+ end
252
+
253
+ class MissingKeyError < RuntimeError
254
+ end
data/lib/ezcrypto.rb CHANGED
@@ -3,7 +3,7 @@ require 'digest/sha2'
3
3
  require 'digest/sha1'
4
4
  require 'base64'
5
5
 
6
- module EzCrypto
6
+ module EzCrypto #:nodoc:
7
7
 
8
8
 
9
9
  =begin rdoc
@@ -22,7 +22,7 @@ Eg.
22
22
 
23
23
  == License
24
24
 
25
- Action Web Service is released under the MIT license.
25
+ ActiveCrypto and EzCrypto are released under the MIT license.
26
26
 
27
27
 
28
28
  == Support
@@ -195,7 +195,7 @@ You probably should be using the Key class instead.
195
195
  Warning! The interface may change.
196
196
 
197
197
  =end
198
- class CipherWrapper
198
+ class CipherWrapper #:nodoc:
199
199
 
200
200
  =begin rdoc
201
201
 
@@ -262,7 +262,7 @@ You probably should be using Key instead.
262
262
  Warning! The interface may change.
263
263
 
264
264
  =end
265
- class Encrypter<EzCrypto::CipherWrapper
265
+ class Encrypter<EzCrypto::CipherWrapper #:nodoc:
266
266
 
267
267
  =begin rdoc
268
268
 
@@ -286,7 +286,7 @@ You probably should be using Key instead.
286
286
 
287
287
  Warning! The interface may change.
288
288
  =end
289
- class Decrypter<EzCrypto::CipherWrapper
289
+ class Decrypter<EzCrypto::CipherWrapper #:nodoc:
290
290
  =begin rdoc
291
291
 
292
292
  =end
data/rakefile CHANGED
@@ -8,7 +8,7 @@ require 'rake/contrib/rubyforgepublisher'
8
8
 
9
9
  PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
10
10
  PKG_NAME = 'ezcrypto'
11
- PKG_VERSION = '0.1.1' + PKG_BUILD
11
+ PKG_VERSION = '0.2' + PKG_BUILD
12
12
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
13
13
 
14
14
  RELEASE_NAME = "REL #{PKG_VERSION}"
@@ -34,7 +34,10 @@ Rake::RDocTask.new { |rdoc|
34
34
  rdoc.options << '--line-numbers --inline-source --main README'
35
35
  rdoc.template = "#{ENV['template']}.rb" if ENV['template']
36
36
  rdoc.rdoc_files.include('README')
37
+ rdoc.rdoc_files.include('README_ACTIVE_CRYPTO')
38
+ rdoc.rdoc_files.include('CHANGELOG')
37
39
  rdoc.rdoc_files.include('lib/ezcrypto.rb')
40
+ rdoc.rdoc_files.include('lib/active_crypto.rb')
38
41
  # rdoc.rdoc_files.include('lib/ezcrypto/*.rb')
39
42
  }
40
43
 
@@ -57,7 +60,7 @@ spec = Gem::Specification.new do |s|
57
60
  s.requirements << 'none'
58
61
  s.require_path = 'lib'
59
62
 
60
- s.files = [ "rakefile", "README", "MIT-LICENSE" ]
63
+ s.files = [ "rakefile", "README", "README_ACTIVE_CRYPTO", "MIT-LICENSE","CHANGELOG" ]
61
64
  s.files = s.files + Dir.glob( "lib/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
62
65
  s.files = s.files + Dir.glob( "test/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
63
66
  end
data/test/CVS/Entries CHANGED
@@ -1 +1,2 @@
1
+ /ezcrypto_test.rb/1.1.1.1/Wed Jul 20 18:40:51 2005//
1
2
  D
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: ezcrypto
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2005-08-27
6
+ version: "0.2"
7
+ date: 2005-10-30
8
8
  summary: Simplified encryption library.
9
9
  require_paths:
10
10
  - lib
@@ -29,7 +29,10 @@ authors:
29
29
  files:
30
30
  - rakefile
31
31
  - README
32
+ - README_ACTIVE_CRYPTO
32
33
  - MIT-LICENSE
34
+ - CHANGELOG
35
+ - lib/active_crypto.rb
33
36
  - lib/CVS
34
37
  - lib/ezcrypto.rb
35
38
  - lib/CVS/Entries
@@ -37,7 +40,6 @@ files:
37
40
  - lib/CVS/Root
38
41
  - test/CVS
39
42
  - test/ezcrypto_test.rb
40
- - test/fixtures
41
43
  - test/CVS/Entries
42
44
  - test/CVS/Repository
43
45
  - test/CVS/Root