ezcrypto 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2004 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/README ADDED
@@ -0,0 +1,130 @@
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
+ == Simple examples
13
+
14
+ ==== To encrypt:
15
+
16
+ Generate a key using a password and a salt. Use the keys encrypt method to encrypt a strings worth of data:
17
+
18
+ @key=EzCrypto::Key.with_password "password", "system salt"
19
+ @encrypted=@key.encrypt "Top secret should not be revealed"
20
+
21
+ ==== To decrypt:
22
+
23
+ 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:
24
+
25
+ @key=EzCrypto::Key.with_password "password", "system salt"
26
+ @key.decrypt @encrypted
27
+
28
+ ==== One liners:
29
+
30
+ These simple examples use one line each:
31
+
32
+ @encrypted=EzCrypto::Key.encrypt_with_password "password", @salt,"Top secret should not be revealed"
33
+
34
+ EzCrypto::Key.decrypt_with_password "password", @salt,@encrypted
35
+
36
+ == Keys
37
+
38
+ 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.
39
+
40
+ ==== Generating a random key
41
+
42
+ The most secure type of key is the randomly generated key:
43
+
44
+ @key=EzCrypto::Key.generate
45
+
46
+ ==== Initializing a key with raw key data
47
+
48
+ If you already have a key from some other source, you simply have to call the constructor with the raw data:
49
+
50
+ @key=EzCrypto::Key.new @binarykey
51
+
52
+ ==== Initializing a Key with a Base64 encoded key
53
+
54
+ 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:
55
+
56
+ @key=EzCrypto::Key.with_password "Secret password"
57
+
58
+ ==== Initializing a Key with a Base64 encoded key
59
+
60
+ If you already have a key from some other source in the popular Base64 encoded format, you use the decode class method:
61
+
62
+ @key=EzCrypto::Key.decode @binarykey
63
+
64
+ ==== Exporting the key
65
+
66
+ 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.
67
+
68
+ puts @key.encode
69
+ puts @key.raw
70
+
71
+ The raw method could be used for storing in a database using a tinyblob column.
72
+
73
+ == Encryption and Decryption
74
+
75
+ 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.
76
+
77
+ ==== Regular raw use
78
+
79
+ Assuming you have generated a key using one of the above methods:
80
+
81
+ @encrypted=@key.encrypt("clear text")
82
+ @decrypted=@key.decrypt(@encrypted)
83
+ assert "clear text", @decrypted
84
+
85
+ ==== Base64 encoded use
86
+
87
+ This uses the encrypt64 and decrypt64 methods. Otherwise it is all the same:
88
+
89
+ @encrypted=@key.encrypt64("clear text")
90
+ @decrypted=@key.decrypt64(@encrypted)
91
+ assert "clear text", @decrypted
92
+
93
+ == FAQ
94
+
95
+ === What algorithm does this use?
96
+
97
+ 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:
98
+
99
+ http://en.wikipedia.org/wiki/AES
100
+
101
+ === Only 128 bits. Is that enough?
102
+
103
+ 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.
104
+
105
+ === What is Base64 encoding?
106
+
107
+ 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.
108
+
109
+ === What is a Salt?
110
+
111
+ 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.
112
+
113
+
114
+ == License
115
+
116
+ Action Web Service is released under the MIT license.
117
+
118
+
119
+ == Support
120
+
121
+ To contact the author, send mail to pelleb@gmail.com
122
+
123
+ Also see my blogs at:
124
+ http://stakeventures.com and
125
+ http://neubia.com
126
+
127
+ This project was based on code used in my project StakeItOut, where you can securely share web services with your partners.
128
+ https://stakeitout.com
129
+
130
+ (C) 2005 Pelle Braendgaard
@@ -0,0 +1 @@
1
+ D
@@ -0,0 +1 @@
1
+ ezcrypto/lib
@@ -0,0 +1 @@
1
+ :ext:pelleb@rubyforge.net:/var/cvs/ezcrypto
@@ -0,0 +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
+
@@ -0,0 +1,195 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/contrib/rubyforgepublisher'
8
+
9
+ PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
10
+ PKG_NAME = 'ezcrypto'
11
+ PKG_VERSION = '0.1' + PKG_BUILD
12
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
13
+
14
+ RELEASE_NAME = "REL #{PKG_VERSION}"
15
+
16
+ RUBY_FORGE_PROJECT = "ezcrypto"
17
+ RUBY_FORGE_USER = "pelleb"
18
+
19
+ desc "Default Task"
20
+ task :default => [ :test ]
21
+
22
+ # Run the unit tests
23
+ Rake::TestTask.new { |t|
24
+ t.libs << "test"
25
+ t.pattern = 'test/*_test.rb'
26
+ t.verbose = true
27
+ }
28
+
29
+
30
+ # Genereate the RDoc documentation
31
+ Rake::RDocTask.new { |rdoc|
32
+ rdoc.rdoc_dir = 'doc'
33
+ rdoc.title = "EzCrypto - Simplified Crypto Library"
34
+ rdoc.options << '--line-numbers --inline-source --main README'
35
+ rdoc.template = "#{ENV['template']}.rb" if ENV['template']
36
+ rdoc.rdoc_files.include('README')
37
+ rdoc.rdoc_files.include('lib/ezcrypto.rb')
38
+ # rdoc.rdoc_files.include('lib/ezcrypto/*.rb')
39
+ }
40
+
41
+
42
+ # Create compressed packages
43
+ spec = Gem::Specification.new do |s|
44
+ s.platform = Gem::Platform::RUBY
45
+ s.name = PKG_NAME
46
+ s.summary = "Simplified encryption library."
47
+ s.description = %q{Makes it easier and safer to write crypto code.}
48
+ s.version = PKG_VERSION
49
+
50
+ s.author = "Pelle Braendgaard"
51
+ s.email = "pelle@stakeitout.com"
52
+ s.rubyforge_project = "ezcrypto"
53
+ s.homepage = "http://ezcrypto.rubyforge.org"
54
+
55
+
56
+ s.has_rdoc = true
57
+ s.requirements << 'none'
58
+ s.require_path = 'lib'
59
+
60
+ s.files = [ "rakefile", "README", "MIT-LICENSE" ]
61
+ s.files = s.files + Dir.glob( "lib/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
62
+ s.files = s.files + Dir.glob( "test/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
63
+ end
64
+
65
+ Rake::GemPackageTask.new(spec) do |p|
66
+ p.gem_spec = spec
67
+ p.need_tar = true
68
+ p.need_zip = true
69
+ end
70
+
71
+
72
+ desc "Publish the API documentation"
73
+ task :pgem => [:package] do
74
+ Rake::SshFilePublisher.new("pelleb@rubyforge.org", "/var/www/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
75
+ end
76
+
77
+ desc "Publish the API documentation"
78
+ task :pdoc => [:rdoc] do
79
+ Rake::SshDirPublisher.new("pelleb@rubyforge.org", "/var/www/gforge-projects/ezcrypto", "doc").upload
80
+ end
81
+
82
+ desc "Publish the release files to RubyForge."
83
+ task :release => [:package] do
84
+ files = ["gem", "tgz", "zip"].map { |ext| "pkg/#{PKG_FILE_NAME}.#{ext}" }
85
+
86
+ if RUBY_FORGE_PROJECT then
87
+ require 'net/http'
88
+ require 'open-uri'
89
+
90
+ project_uri = "http://rubyforge.org/projects/#{RUBY_FORGE_PROJECT}/"
91
+ project_data = open(project_uri) { |data| data.read }
92
+ group_id = project_data[/[?&]group_id=(\d+)/, 1]
93
+ raise "Couldn't get group id" unless group_id
94
+
95
+ # This echos password to shell which is a bit sucky
96
+ if ENV["RUBY_FORGE_PASSWORD"]
97
+ password = ENV["RUBY_FORGE_PASSWORD"]
98
+ else
99
+ print "#{RUBY_FORGE_USER}@rubyforge.org's password: "
100
+ password = STDIN.gets.chomp
101
+ end
102
+
103
+ login_response = Net::HTTP.start("rubyforge.org", 80) do |http|
104
+ data = [
105
+ "login=1",
106
+ "form_loginname=#{RUBY_FORGE_USER}",
107
+ "form_pw=#{password}"
108
+ ].join("&")
109
+ http.post("/account/login.php", data)
110
+ end
111
+
112
+ cookie = login_response["set-cookie"]
113
+ raise "Login failed" unless cookie
114
+ headers = { "Cookie" => cookie }
115
+
116
+ release_uri = "http://rubyforge.org/frs/admin/?group_id=#{group_id}"
117
+ release_data = open(release_uri, headers) { |data| data.read }
118
+ package_id = release_data[/[?&]package_id=(\d+)/, 1]
119
+ raise "Couldn't get package id" unless package_id
120
+
121
+ first_file = true
122
+ release_id = ""
123
+
124
+ files.each do |filename|
125
+ basename = File.basename(filename)
126
+ file_ext = File.extname(filename)
127
+ file_data = File.open(filename, "rb") { |file| file.read }
128
+
129
+ puts "Releasing #{basename}..."
130
+
131
+ release_response = Net::HTTP.start("rubyforge.org", 80) do |http|
132
+ release_date = Time.now.strftime("%Y-%m-%d %H:%M")
133
+ type_map = {
134
+ ".zip" => "3000",
135
+ ".tgz" => "3110",
136
+ ".gz" => "3110",
137
+ ".gem" => "1400"
138
+ }; type_map.default = "9999"
139
+ type = type_map[file_ext]
140
+ boundary = "rubyqMY6QN9bp6e4kS21H4y0zxcvoor"
141
+
142
+ query_hash = if first_file then
143
+ {
144
+ "group_id" => group_id,
145
+ "package_id" => package_id,
146
+ "release_name" => RELEASE_NAME,
147
+ "release_date" => release_date,
148
+ "type_id" => type,
149
+ "processor_id" => "8000", # Any
150
+ "release_notes" => "",
151
+ "release_changes" => "",
152
+ "preformatted" => "1",
153
+ "submit" => "1"
154
+ }
155
+ else
156
+ {
157
+ "group_id" => group_id,
158
+ "release_id" => release_id,
159
+ "package_id" => package_id,
160
+ "step2" => "1",
161
+ "type_id" => type,
162
+ "processor_id" => "8000", # Any
163
+ "submit" => "Add This File"
164
+ }
165
+ end
166
+
167
+ query = "?" + query_hash.map do |(name, value)|
168
+ [name, URI.encode(value)].join("=")
169
+ end.join("&")
170
+
171
+ data = [
172
+ "--" + boundary,
173
+ "Content-Disposition: form-data; name=\"userfile\"; filename=\"#{basename}\"",
174
+ "Content-Type: application/octet-stream",
175
+ "Content-Transfer-Encoding: binary",
176
+ "", file_data, ""
177
+ ].join("\x0D\x0A")
178
+
179
+ release_headers = headers.merge(
180
+ "Content-Type" => "multipart/form-data; boundary=#{boundary}"
181
+ )
182
+
183
+ target = first_file ? "/frs/admin/qrs.php" : "/frs/admin/editrelease.php"
184
+ http.post(target + query, data, release_headers)
185
+ end
186
+
187
+ if first_file then
188
+ release_id = release_response.body[/release_id=(\d+)/, 1]
189
+ raise("Couldn't get release id") unless release_id
190
+ end
191
+
192
+ first_file = false
193
+ end
194
+ end
195
+ end
@@ -0,0 +1 @@
1
+ D
@@ -0,0 +1 @@
1
+ ezcrypto/test
@@ -0,0 +1 @@
1
+ :ext:pelleb@rubyforge.net:/var/cvs/ezcrypto
@@ -0,0 +1,112 @@
1
+ $:.unshift(File.dirname(__FILE__) + "/../lib/")
2
+
3
+ require 'test/unit'
4
+ require 'ezcrypto'
5
+ require 'base64'
6
+
7
+ class EzCryptoTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ end
11
+
12
+ def test_generate_alg_key
13
+ assert_generate_alg_key "aes-128-cbc",16
14
+ assert_generate_alg_key "aes-192-cbc",24
15
+ assert_generate_alg_key "aes-256-cbc",32
16
+ assert_generate_alg_key "rc2-40-cbc",5
17
+ assert_generate_alg_key "rc2-64-cbc",8
18
+ assert_generate_alg_key "rc4-64" ,8
19
+ assert_generate_alg_key "blowfish" ,16
20
+ assert_generate_alg_key "des" ,8
21
+ end
22
+
23
+ def test_with_password
24
+ assert_with_password "","secret","aes-128-cbc",16
25
+ assert_with_password "test","secret","aes-128-cbc",16
26
+ assert_with_password "password","secret","aes-128-cbc",16
27
+ assert_with_password "a�sldfad8q5�34j2�l4j24l6j2456","secret","aes-128-cbc",16
28
+
29
+ assert_with_password "","secret","aes-192-cbc",24
30
+ assert_with_password "test","secret","aes-192-cbc",24
31
+ assert_with_password "password","secret","aes-192-cbc",24
32
+ assert_with_password "a�sldfad8q5�34j2�l4j24l6j2456","secret","aes-192-cbc",24
33
+
34
+ assert_with_password "","secret","aes-256-cbc",32
35
+ assert_with_password "test","secret","aes-256-cbc",32
36
+ assert_with_password "password","secret","aes-256-cbc",32
37
+ assert_with_password "a�sldfad8q5�34j2�l4j24l6j2456","secret","aes-256-cbc",32
38
+
39
+ end
40
+
41
+ def test_encoded
42
+ 0.upto 32 do |size|
43
+ assert_encoded_keys size
44
+ end
45
+ end
46
+
47
+ def test_encrypt
48
+ 0.upto(CLEAR_TEXT.size-1) do |size|
49
+ assert_encrypt CLEAR_TEXT[0..size]
50
+ end
51
+ end
52
+
53
+ def test_decrypt
54
+
55
+ 0.upto(CLEAR_TEXT.size) do |size|
56
+ assert_decrypt CLEAR_TEXT[0..size]
57
+ end
58
+ end
59
+
60
+ def test_decrypt64
61
+ 0.upto(CLEAR_TEXT.size) do |size|
62
+ assert_decrypt64 CLEAR_TEXT[0..size]
63
+ end
64
+ end
65
+
66
+ def assert_key_size(size,key)
67
+ assert_equal size,key.raw.size
68
+ end
69
+
70
+ def assert_generate_alg_key(algorithm,size)
71
+ key=EzCrypto::Key.generate :algorithm=>algorithm
72
+ assert_key_size size,key
73
+ end
74
+
75
+ def assert_with_password(password,salt,algorithm,size)
76
+ key=EzCrypto::Key.with_password password,salt,:algorithm=>algorithm
77
+ assert_key_size size,key
78
+ assert_equal key.raw,EzCrypto::Key.with_password( password,salt,:algorithm=>algorithm).raw
79
+ end
80
+
81
+ def assert_encoded_keys(size)
82
+ key=EzCrypto::Key.generate size
83
+ key2=EzCrypto::Key.decode(key.encode)
84
+ assert_equal key.raw, key2.raw
85
+ end
86
+
87
+ def assert_encrypt(clear)
88
+ ALGORITHMS.each do |alg|
89
+ key=EzCrypto::Key.generate :algorithm=>alg
90
+ encrypted=key.encrypt clear
91
+ assert_not_nil encrypted
92
+ end
93
+ end
94
+
95
+ def assert_decrypt(clear)
96
+ ALGORITHMS.each do |alg|
97
+ key=EzCrypto::Key.generate :algorithm=>alg
98
+ encrypted=key.encrypt clear
99
+ assert_not_nil encrypted
100
+ assert_equal clear,key.decrypt(encrypted)
101
+ end
102
+ end
103
+ def assert_decrypt64(clear)
104
+ key=EzCrypto::Key.generate
105
+ encrypted=key.encrypt64 clear
106
+ assert_not_nil encrypted
107
+ assert_equal clear,key.decrypt64(encrypted)
108
+ end
109
+ ALGORITHMS=["aes128","bf","blowfish","des","des3","rc4","rc2"]
110
+ CLEAR_TEXT="Lorem ipsum dolor sit amet, suspendisse id interdum mus leo id. Sapien tempus consequat nullam, platea vitae sociis sed elementum et fermentum, vel praesent eget. Sed blandit augue, molestie mus sed habitant, semper voluptatibus neque, nullam a augue. Aptent imperdiet curabitur, quam quis laoreet. Dolor magna. Quis vestibulum amet eu arcu fringilla nibh, mi urna sunt dictumst nulla, elit quisque purus eros, sem hendrerit. Vulputate tortor rhoncus ac nonummy tortor nulla. Nunc id nunc luctus ligula."
111
+ end
112
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: ezcrypto
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2005-07-20
8
+ summary: Simplified encryption library.
9
+ require_paths:
10
+ - lib
11
+ email: pelle@stakeitout.com
12
+ homepage: http://ezcrypto.rubyforge.org
13
+ rubyforge_project: ezcrypto
14
+ description: Makes it easier and safer to write crypto code.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Pelle Braendgaard
29
+ files:
30
+ - rakefile
31
+ - README
32
+ - MIT-LICENSE
33
+ - lib/CVS
34
+ - lib/ezcrypto.rb
35
+ - lib/CVS/Entries
36
+ - lib/CVS/Repository
37
+ - lib/CVS/Root
38
+ - test/CVS
39
+ - test/ezcrypto_test.rb
40
+ - test/fixtures
41
+ - test/CVS/Entries
42
+ - test/CVS/Repository
43
+ - test/CVS/Root
44
+ test_files: []
45
+ rdoc_options: []
46
+ extra_rdoc_files: []
47
+ executables: []
48
+ extensions: []
49
+ requirements:
50
+ - none
51
+ dependencies: []