shuber-encryptor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ 2009-01-06 - Sean Huber (shuber@huberry.com)
2
+ * Initial commit
3
+ * Add tests
4
+ * Update README
5
+ * Add gemspec
6
+ * Modify gemspec so github will rebuild the gem
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Sean Huber - shuber@huberry.com
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.
data/README.markdown ADDED
@@ -0,0 +1,32 @@
1
+ Encryptor
2
+ =========
3
+
4
+ A simple wrapper for the standard ruby OpenSSL library
5
+
6
+
7
+ Installation
8
+ ------------
9
+
10
+ gem install shuber-encryptor --source http://gems.github.com
11
+
12
+
13
+ Usage
14
+ -----
15
+
16
+ secret_key = Digest::SHA256.hexdigest('a secret key')
17
+ encrypted_value = Huberry::Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
18
+ decrypted_value = Huberry::Encryptor.decrypt(:value => encrypted_value, :key => secret_key) # 'some string to encrypt'
19
+
20
+ You may also pass the `:iv` and `:algorithm` options but they are not required. If an algorithm is not specified, the Encryptor uses
21
+ the algorithm found at `Huberry::Encryptor.default_algorithm` which is `aes-256-cbc` by default. You can change the default algorithm
22
+ by overwriting this attribute:
23
+
24
+ Huberry::Encryptor.default_algorithm = 'bf'
25
+
26
+ Run `openssl list-cipher-commands` in your terminal to view a list all cipher algorithms that are supported on your platform.
27
+
28
+
29
+ Contact
30
+ -------
31
+
32
+ Problems, comments, and suggestions all welcome: [shuber@huberry.com](mailto:shuber@huberry.com)
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the encryptor gem.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the encryptor gem.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Encryptor'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README.markdown')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/lib/encryptor.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'openssl'
2
+
3
+ module Huberry
4
+ module Encryptor
5
+ # The default algorithm to use if one is not explicitly passed as an option to the <tt>encrypt</tt> or <tt>decrypt</tt> methods
6
+ #
7
+ # Defaults to 'aes-256-cbc'
8
+ #
9
+ # Run 'openssl list-cipher-commands' in your terminal to view a list all cipher algorithms that are supported on your platform
10
+ class << self; attr_accessor :default_algorithm; end
11
+ self.default_algorithm = 'aes-256-cbc'
12
+
13
+ # Encrypts a <tt>:value</tt> with a specified <tt>:key</tt>
14
+ #
15
+ # Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
16
+ #
17
+ # Example
18
+ #
19
+ # encrypted_value = Huberry::Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
20
+ def self.encrypt(options)
21
+ crypt :encrypt, options
22
+ end
23
+
24
+ # Decrypts a <tt>:value</tt> with a specified <tt>:key</tt>
25
+ #
26
+ # Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
27
+ #
28
+ # Example
29
+ #
30
+ # decrypted_value = Huberry::Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
31
+ def self.decrypt(options)
32
+ crypt :decrypt, options
33
+ end
34
+
35
+ protected
36
+
37
+ def self.crypt(cipher_method, options = {})
38
+ cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm] || default_algorithm)
39
+ cipher.send(cipher_method)
40
+ if options[:iv]
41
+ cipher.key = options[:key]
42
+ cipher.iv = options[:iv]
43
+ else
44
+ cipher.pkcs5_keyivgen(options[:key])
45
+ end
46
+ result = cipher.update(options[:value])
47
+ result << cipher.final
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+ require 'digest/sha2'
3
+ require File.dirname(__FILE__) + '/../lib/encryptor'
4
+
5
+ class EncryptorTest < Test::Unit::TestCase
6
+
7
+ algorithms = %x(openssl list-cipher-commands).split
8
+ original_value = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
9
+ key = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
10
+ iv = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
11
+
12
+ algorithms.each do |algorithm|
13
+ define_method "test_should_crypt_with_#{algorithm}_algorithm_with_iv" do
14
+ encrypted_value = Huberry::Encryptor.encrypt(:value => original_value, :key => key, :iv => iv, :algorithm => algorithm)
15
+ assert_not_equal original_value, encrypted_value
16
+ assert_not_equal Huberry::Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm), encrypted_value
17
+ assert_equal original_value, Huberry::Encryptor.decrypt(:value => encrypted_value, :key => key, :iv => iv, :algorithm => algorithm)
18
+ end
19
+
20
+ define_method "test_should_crypt_with_#{algorithm}_algorithm_without_iv" do
21
+ encrypted_value = Huberry::Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm)
22
+ assert_not_equal original_value, encrypted_value
23
+ assert_equal original_value, Huberry::Encryptor.decrypt(:value => encrypted_value, :key => key, :algorithm => algorithm)
24
+ end
25
+ end
26
+
27
+ define_method 'test_should_have_a_default_algorithm' do
28
+ assert algorithms.include?(Huberry::Encryptor.default_algorithm)
29
+ end
30
+
31
+ define_method 'test_should_use_the_default_algorithm_if_one_is_not_specified' do
32
+ assert_equal Huberry::Encryptor.encrypt(:value => original_value, :key => key, :algorithm => Huberry::Encryptor.default_algorithm), Huberry::Encryptor.encrypt(:value => original_value, :key => key)
33
+ end
34
+
35
+ def test_should_be_able_to_change_the_default_algorithm
36
+ original_algorithm = Huberry::Encryptor.default_algorithm
37
+ assert_not_equal 'test', original_algorithm
38
+ Huberry::Encryptor.default_algorithm = 'test'
39
+ assert_equal 'test', Huberry::Encryptor.default_algorithm
40
+ Huberry::Encryptor.default_algorithm = original_algorithm
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shuber-encryptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Huber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple wrapper for the standard ruby OpenSSL library
17
+ email: shuber@huberry.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - CHANGELOG
26
+ - lib/encryptor.rb
27
+ - MIT-LICENSE
28
+ - Rakefile
29
+ - README.markdown
30
+ has_rdoc: false
31
+ homepage: http://github.com/shuber/encryptor
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --line-numbers
35
+ - --inline-source
36
+ - --main
37
+ - README.markdown
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A simple wrapper for the standard ruby OpenSSL library
59
+ test_files:
60
+ - test/encryptor_test.rb