ezcrypto 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ezcrypto.rb +357 -357
  2. data/rakefile +2 -2
  3. metadata +2 -2
data/lib/ezcrypto.rb CHANGED
@@ -1,357 +1,357 @@
1
- require 'openssl'
2
- require 'digest/sha2'
3
- require 'digest/sha1'
4
- require 'base64'
5
-
6
- module EzCrypto
7
-
8
-
9
- =begin rdoc
10
- The Key is the only class you need to understand for simple use.
11
-
12
- === Algorithms
13
-
14
- The crypto algorithms default to aes-128-cbc however on any of the class methods you can change it to one of the standard openssl cipher names using the
15
- optional <tt>:algorithm=>alg name</tt> parameter.
16
-
17
- Eg.
18
- Key.new @raw, :algorithm=>"des"
19
- Key.generate :algorithm=>"blowfish"
20
- Key.with_password @pwd,@salt,:algorithm=>"aes256"
21
-
22
-
23
- == License
24
-
25
- Action Web Service is released under the MIT license.
26
-
27
-
28
- == Support
29
-
30
- To contact the author, send mail to pelleb@gmail.com
31
-
32
- Also see my blogs at:
33
- http://stakeventures.com and
34
- http://neubia.com
35
-
36
- This project was based on code used in my project StakeItOut, where you can securely share web services with your partners.
37
- https://stakeitout.com
38
-
39
- (C) 2005 Pelle Braendgaard
40
-
41
- =end
42
-
43
- class Key
44
- attr_reader :raw,:algorithm
45
-
46
- =begin rdoc
47
- Initialize the key with raw binary key data. This needs to be at least
48
- 16 bytes long for the default aes-128 algorithm.
49
- =end
50
- def initialize(raw,options = {})
51
- @raw=raw
52
- @algorithm=options[:algorithm]||"aes-128-cbc"
53
- end
54
-
55
- =begin rdoc
56
- Generate random key.
57
- =end
58
- def self.generate(options = {})
59
- Key.new(EzCrypto::Digester.generate_key(calculate_key_size(options[:algorithm])),options)
60
- end
61
-
62
- =begin rdoc
63
- Create key generated from the given password and salt
64
- =end
65
- def self.with_password(password,salt,options = {})
66
- Key.new(EzCrypto::Digester.get_key(password,salt,calculate_key_size(options[:algorithm])),options)
67
- end
68
-
69
- =begin rdoc
70
- Initialize the key with Base64 encoded key data.
71
- =end
72
- def self.decode(encoded,options = {})
73
- Key.new(Base64.decode64(encoded),options)
74
- end
75
-
76
- =begin rdoc
77
- Encrypts the data with the given password and a salt. Short hand for:
78
-
79
- key=Key.with_password(password,salt,options)
80
- key.encrypt(data)
81
-
82
- =end
83
- def self.encrypt_with_password(password,salt,data,options = {})
84
- key=Key.with_password(password,salt,options)
85
- key.encrypt(data)
86
- end
87
-
88
- =begin rdoc
89
- Decrypts the data with the given password and a salt. Short hand for:
90
-
91
- key=Key.with_password(password,salt,options)
92
- key.decrypt(data)
93
-
94
-
95
- =end
96
- def self.decrypt_with_password(password,salt,data,options = {})
97
- key=Key.with_password(password,salt,options)
98
- key.decrypt(data)
99
- end
100
-
101
- =begin rdoc
102
- Given an algorithm this calculates the keysize. This is used by both the generate and with_password methods. This is not yet 100% complete.
103
- =end
104
- def self.calculate_key_size(algorithm)
105
- if !algorithm.nil?
106
- algorithm=~/^([[:alnum:]]+)(-(\d+))?/
107
- if $3
108
- size=($3.to_i)/8
109
- else
110
- case $1
111
- when "bf"
112
- size = 16
113
- when "blowfish"
114
- size = 16
115
- when "des"
116
- size = 8
117
- when "des3"
118
- size = 24
119
- when "aes128"
120
- size = 16
121
- when "aes192"
122
- size = 24
123
- when "aes256"
124
- size = 32
125
- when "rc2"
126
- size = 16
127
- when "rc4"
128
- size = 16
129
- else
130
- size = 16
131
- end
132
- end
133
- end
134
- if size.nil?
135
- size = 16
136
- end
137
-
138
- size
139
- end
140
-
141
- =begin rdoc
142
- returns the Base64 encoded key.
143
- =end
144
- def encode
145
- Base64.encode64 @raw
146
- end
147
-
148
- =begin rdoc
149
- returns the Base64 encoded key. Synonymo for encode.
150
- =end
151
- def to_s
152
- encoded
153
- end
154
-
155
- =begin rdoc
156
- Encrypts the data and returns it in encrypted binary form.
157
- =end
158
- def encrypt(data)
159
- @cipher=EzCrypto::Encrypter.new(self,"",@algorithm)
160
- @cipher.encrypt(data)
161
- end
162
-
163
- =begin rdoc
164
- Encrypts the data and returns it in encrypted Base64 encoded form.
165
- =end
166
- def encrypt64(data)
167
- Base64.encode64(encrypt(data))
168
- end
169
-
170
- =begin rdoc
171
- Decrypts the data passed to it in binary format.
172
- =end
173
- def decrypt(data)
174
- @cipher=EzCrypto::Decrypter.new(self,"",@algorithm)
175
- @cipher.gulp(data)
176
- rescue
177
- puts @algorithm
178
- throw $!
179
- end
180
-
181
- =begin rdoc
182
- Decrypts a Base64 formatted string
183
- =end
184
- def decrypt64(data)
185
- decrypt(Base64.decode64(data))
186
- end
187
-
188
-
189
- end
190
- =begin rdoc
191
- Abstract Wrapper around OpenSSL's Cipher object. Extended by Encrypter and Decrypter.
192
-
193
- You probably should be using the Key class instead.
194
-
195
- Warning! The interface may change.
196
-
197
- =end
198
- class CipherWrapper
199
-
200
- =begin rdoc
201
-
202
- =end
203
- def initialize(key,target,mode,algorithm)
204
- @cipher = OpenSSL::Cipher::Cipher.new(algorithm)
205
- if mode
206
- @cipher.encrypt
207
- else
208
- @cipher.decrypt
209
- end
210
- @cipher.key=key.raw
211
- @cipher.padding=1
212
- @target=target
213
- @finished=false
214
- end
215
-
216
- =begin rdoc
217
- Process the givend data with the cipher.
218
- =end
219
- def update(data)
220
- reset if @finished
221
- @target<< @cipher.update(data)
222
- end
223
-
224
- =begin rdoc
225
-
226
- =end
227
- def <<(data)
228
- update(data)
229
- end
230
-
231
- =begin rdoc
232
- Finishes up any last bits of data in the cipher and returns the final result.
233
- =end
234
- def final
235
- @target<< @cipher.final
236
- @finished=true
237
- @target
238
- end
239
-
240
- =begin rdoc
241
- Processes the entire data string using update and performs a final on it returning the data.
242
- =end
243
- def gulp(data)
244
- update(data)
245
- final
246
- end
247
-
248
- =begin rdoc
249
-
250
- =end
251
- def reset(target="")
252
- @target=target
253
- @finished=false
254
- end
255
- end
256
-
257
- =begin rdoc
258
- Wrapper around OpenSSL Cipher for Encryption use.
259
-
260
- You probably should be using Key instead.
261
-
262
- Warning! The interface may change.
263
-
264
- =end
265
- class Encrypter<EzCrypto::CipherWrapper
266
-
267
- =begin rdoc
268
-
269
- =end
270
- def initialize(key,target="",algorithm="aes-128-cbc")
271
- super(key,target,true,algorithm)
272
- end
273
-
274
- =begin rdoc
275
-
276
- =end
277
- def encrypt(data)
278
- gulp(data)
279
- end
280
- end
281
-
282
- =begin rdoc
283
- Wrapper around OpenSSL Cipher for Decryption use.
284
-
285
- You probably should be using Key instead.
286
-
287
- Warning! The interface may change.
288
- =end
289
- class Decrypter<EzCrypto::CipherWrapper
290
- =begin rdoc
291
-
292
- =end
293
- def initialize(key,target="",algorithm="aes-128-cbc")
294
- super(key,target,false,algorithm)
295
- end
296
-
297
- =begin rdoc
298
-
299
- =end
300
- def decrypt(data)
301
- gulp(data)
302
- end
303
- end
304
-
305
- =begin rdoc
306
-
307
- =end
308
- class Digester
309
- =begin rdoc
310
- Various handy Digest methods.
311
-
312
- Warning! The interface may change.
313
- =end
314
- def self.get_key(password,salt,size)
315
- digest(salt+password,size)
316
- end
317
-
318
- =begin rdoc
319
-
320
- =end
321
- def self.generate_key(size=16)
322
- key=OpenSSL::Random.random_bytes(size)
323
- digest(key,size)
324
- end
325
-
326
- =begin rdoc
327
-
328
- =end
329
- def self.generate_key64(size=32)
330
- key=OpenSSL::Random.random_bytes(size)
331
- digest(key,size)
332
- end
333
-
334
- =begin rdoc
335
-
336
- =end
337
- def self.digest(data,size=16)
338
- if size==0
339
- ""
340
- elsif size<=16
341
- Digest::SHA1.digest(data)[0..(size-1)]
342
- else
343
- Digest::SHA256.digest(data)[0..(size-1)]
344
- end
345
- end
346
-
347
- =begin rdoc
348
-
349
- =end
350
- def self.digest64(data)
351
- Base64.encode64(digest(data))
352
- end
353
- end
354
-
355
- end
356
-
357
-
1
+ require 'openssl'
2
+ require 'digest/sha2'
3
+ require 'digest/sha1'
4
+ require 'base64'
5
+
6
+ module EzCrypto
7
+
8
+
9
+ =begin rdoc
10
+ The Key is the only class you need to understand for simple use.
11
+
12
+ === Algorithms
13
+
14
+ The crypto algorithms default to aes-128-cbc however on any of the class methods you can change it to one of the standard openssl cipher names using the
15
+ optional <tt>:algorithm=>alg name</tt> parameter.
16
+
17
+ Eg.
18
+ Key.new @raw, :algorithm=>"des"
19
+ Key.generate :algorithm=>"blowfish"
20
+ Key.with_password @pwd,@salt,:algorithm=>"aes256"
21
+
22
+
23
+ == License
24
+
25
+ Action Web Service is released under the MIT license.
26
+
27
+
28
+ == Support
29
+
30
+ To contact the author, send mail to pelleb@gmail.com
31
+
32
+ Also see my blogs at:
33
+ http://stakeventures.com and
34
+ http://neubia.com
35
+
36
+ This project was based on code used in my project StakeItOut, where you can securely share web services with your partners.
37
+ https://stakeitout.com
38
+
39
+ (C) 2005 Pelle Braendgaard
40
+
41
+ =end
42
+
43
+ class Key
44
+ attr_reader :raw,:algorithm
45
+
46
+ =begin rdoc
47
+ Initialize the key with raw unencoded binary key data. This needs to be at least
48
+ 16 bytes long for the default aes-128 algorithm.
49
+ =end
50
+ def initialize(raw,options = {})
51
+ @raw=raw
52
+ @algorithm=options[:algorithm]||"aes-128-cbc"
53
+ end
54
+
55
+ =begin rdoc
56
+ Generate random key.
57
+ =end
58
+ def self.generate(options = {})
59
+ Key.new(EzCrypto::Digester.generate_key(calculate_key_size(options[:algorithm])),options)
60
+ end
61
+
62
+ =begin rdoc
63
+ Create key generated from the given password and salt
64
+ =end
65
+ def self.with_password(password,salt,options = {})
66
+ Key.new(EzCrypto::Digester.get_key(password,salt,calculate_key_size(options[:algorithm])),options)
67
+ end
68
+
69
+ =begin rdoc
70
+ Initialize the key with Base64 encoded key data.
71
+ =end
72
+ def self.decode(encoded,options = {})
73
+ Key.new(Base64.decode64(encoded),options)
74
+ end
75
+
76
+ =begin rdoc
77
+ Encrypts the data with the given password and a salt. Short hand for:
78
+
79
+ key=Key.with_password(password,salt,options)
80
+ key.encrypt(data)
81
+
82
+ =end
83
+ def self.encrypt_with_password(password,salt,data,options = {})
84
+ key=Key.with_password(password,salt,options)
85
+ key.encrypt(data)
86
+ end
87
+
88
+ =begin rdoc
89
+ Decrypts the data with the given password and a salt. Short hand for:
90
+
91
+ key=Key.with_password(password,salt,options)
92
+ key.decrypt(data)
93
+
94
+
95
+ =end
96
+ def self.decrypt_with_password(password,salt,data,options = {})
97
+ key=Key.with_password(password,salt,options)
98
+ key.decrypt(data)
99
+ end
100
+
101
+ =begin rdoc
102
+ Given an algorithm this calculates the keysize. This is used by both the generate and with_password methods. This is not yet 100% complete.
103
+ =end
104
+ def self.calculate_key_size(algorithm)
105
+ if !algorithm.nil?
106
+ algorithm=~/^([[:alnum:]]+)(-(\d+))?/
107
+ if $3
108
+ size=($3.to_i)/8
109
+ else
110
+ case $1
111
+ when "bf"
112
+ size = 16
113
+ when "blowfish"
114
+ size = 16
115
+ when "des"
116
+ size = 8
117
+ when "des3"
118
+ size = 24
119
+ when "aes128"
120
+ size = 16
121
+ when "aes192"
122
+ size = 24
123
+ when "aes256"
124
+ size = 32
125
+ when "rc2"
126
+ size = 16
127
+ when "rc4"
128
+ size = 16
129
+ else
130
+ size = 16
131
+ end
132
+ end
133
+ end
134
+ if size.nil?
135
+ size = 16
136
+ end
137
+
138
+ size
139
+ end
140
+
141
+ =begin rdoc
142
+ returns the Base64 encoded key.
143
+ =end
144
+ def encode
145
+ Base64.encode64 @raw
146
+ end
147
+
148
+ =begin rdoc
149
+ returns the Base64 encoded key. Synonym for encode.
150
+ =end
151
+ def to_s
152
+ encode
153
+ end
154
+
155
+ =begin rdoc
156
+ Encrypts the data and returns it in encrypted binary form.
157
+ =end
158
+ def encrypt(data)
159
+ @cipher=EzCrypto::Encrypter.new(self,"",@algorithm)
160
+ @cipher.encrypt(data)
161
+ end
162
+
163
+ =begin rdoc
164
+ Encrypts the data and returns it in encrypted Base64 encoded form.
165
+ =end
166
+ def encrypt64(data)
167
+ Base64.encode64(encrypt(data))
168
+ end
169
+
170
+ =begin rdoc
171
+ Decrypts the data passed to it in binary format.
172
+ =end
173
+ def decrypt(data)
174
+ @cipher=EzCrypto::Decrypter.new(self,"",@algorithm)
175
+ @cipher.gulp(data)
176
+ rescue
177
+ puts @algorithm
178
+ throw $!
179
+ end
180
+
181
+ =begin rdoc
182
+ Decrypts a Base64 formatted string
183
+ =end
184
+ def decrypt64(data)
185
+ decrypt(Base64.decode64(data))
186
+ end
187
+
188
+
189
+ end
190
+ =begin rdoc
191
+ Abstract Wrapper around OpenSSL's Cipher object. Extended by Encrypter and Decrypter.
192
+
193
+ You probably should be using the Key class instead.
194
+
195
+ Warning! The interface may change.
196
+
197
+ =end
198
+ class CipherWrapper
199
+
200
+ =begin rdoc
201
+
202
+ =end
203
+ def initialize(key,target,mode,algorithm)
204
+ @cipher = OpenSSL::Cipher::Cipher.new(algorithm)
205
+ if mode
206
+ @cipher.encrypt
207
+ else
208
+ @cipher.decrypt
209
+ end
210
+ @cipher.key=key.raw
211
+ @cipher.padding=1
212
+ @target=target
213
+ @finished=false
214
+ end
215
+
216
+ =begin rdoc
217
+ Process the givend data with the cipher.
218
+ =end
219
+ def update(data)
220
+ reset if @finished
221
+ @target<< @cipher.update(data)
222
+ end
223
+
224
+ =begin rdoc
225
+
226
+ =end
227
+ def <<(data)
228
+ update(data)
229
+ end
230
+
231
+ =begin rdoc
232
+ Finishes up any last bits of data in the cipher and returns the final result.
233
+ =end
234
+ def final
235
+ @target<< @cipher.final
236
+ @finished=true
237
+ @target
238
+ end
239
+
240
+ =begin rdoc
241
+ Processes the entire data string using update and performs a final on it returning the data.
242
+ =end
243
+ def gulp(data)
244
+ update(data)
245
+ final
246
+ end
247
+
248
+ =begin rdoc
249
+
250
+ =end
251
+ def reset(target="")
252
+ @target=target
253
+ @finished=false
254
+ end
255
+ end
256
+
257
+ =begin rdoc
258
+ Wrapper around OpenSSL Cipher for Encryption use.
259
+
260
+ You probably should be using Key instead.
261
+
262
+ Warning! The interface may change.
263
+
264
+ =end
265
+ class Encrypter<EzCrypto::CipherWrapper
266
+
267
+ =begin rdoc
268
+
269
+ =end
270
+ def initialize(key,target="",algorithm="aes-128-cbc")
271
+ super(key,target,true,algorithm)
272
+ end
273
+
274
+ =begin rdoc
275
+
276
+ =end
277
+ def encrypt(data)
278
+ gulp(data)
279
+ end
280
+ end
281
+
282
+ =begin rdoc
283
+ Wrapper around OpenSSL Cipher for Decryption use.
284
+
285
+ You probably should be using Key instead.
286
+
287
+ Warning! The interface may change.
288
+ =end
289
+ class Decrypter<EzCrypto::CipherWrapper
290
+ =begin rdoc
291
+
292
+ =end
293
+ def initialize(key,target="",algorithm="aes-128-cbc")
294
+ super(key,target,false,algorithm)
295
+ end
296
+
297
+ =begin rdoc
298
+
299
+ =end
300
+ def decrypt(data)
301
+ gulp(data)
302
+ end
303
+ end
304
+
305
+ =begin rdoc
306
+
307
+ =end
308
+ class Digester
309
+ =begin rdoc
310
+ Various handy Digest methods.
311
+
312
+ Warning! The interface may change.
313
+ =end
314
+ def self.get_key(password,salt,size)
315
+ digest(salt+password,size)
316
+ end
317
+
318
+ =begin rdoc
319
+
320
+ =end
321
+ def self.generate_key(size=16)
322
+ key=OpenSSL::Random.random_bytes(size)
323
+ digest(key,size)
324
+ end
325
+
326
+ =begin rdoc
327
+
328
+ =end
329
+ def self.generate_key64(size=32)
330
+ key=OpenSSL::Random.random_bytes(size)
331
+ digest(key,size)
332
+ end
333
+
334
+ =begin rdoc
335
+
336
+ =end
337
+ def self.digest(data,size=16)
338
+ if size==0
339
+ ""
340
+ elsif size<=16
341
+ Digest::SHA1.digest(data)[0..(size-1)]
342
+ else
343
+ Digest::SHA256.digest(data)[0..(size-1)]
344
+ end
345
+ end
346
+
347
+ =begin rdoc
348
+
349
+ =end
350
+ def self.digest64(data)
351
+ Base64.encode64(digest(data))
352
+ end
353
+ end
354
+
355
+ end
356
+
357
+
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' + PKG_BUILD
11
+ PKG_VERSION = '0.1.1' + PKG_BUILD
12
12
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
13
13
 
14
14
  RELEASE_NAME = "REL #{PKG_VERSION}"
@@ -192,4 +192,4 @@ task :release => [:package] do
192
192
  first_file = false
193
193
  end
194
194
  end
195
- end
195
+ end
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"
7
- date: 2005-07-20
6
+ version: 0.1.1
7
+ date: 2005-08-27
8
8
  summary: Simplified encryption library.
9
9
  require_paths:
10
10
  - lib