encryptor 1.0.1

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,12 @@
1
+ 2009-02-10 - Sean Huber (shuber@huberry.com)
2
+ * Update README
3
+
4
+ 2009-01-10 - Sean Huber (shuber@huberry.com)
5
+ * Add default_options attribute
6
+
7
+ 2009-01-06 - Sean Huber (shuber@huberry.com)
8
+ * Initial commit
9
+ * Add tests
10
+ * Update README
11
+ * Add gemspec
12
+ * 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,86 @@
1
+ Encryptor
2
+ =========
3
+
4
+ A simple wrapper for the standard ruby OpenSSL library
5
+
6
+ See [http://github.com/shuber/attr_encrypted](http://github.com/shuber/attr_encrypted) to easily encrypt/decrypt attributes in any class
7
+
8
+
9
+ Installation
10
+ ------------
11
+
12
+ gem install shuber-encryptor --source http://gems.github.com
13
+
14
+
15
+ Usage
16
+ -----
17
+
18
+ secret_key = Digest::SHA256.hexdigest('a secret key')
19
+ encrypted_value = Huberry::Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
20
+ decrypted_value = Huberry::Encryptor.decrypt(:value => encrypted_value, :key => secret_key) # 'some string to encrypt'
21
+
22
+ You may also pass the `:iv` and `:algorithm` options but they are not required. If an algorithm is not specified, the Encryptor uses
23
+ the algorithm found at `Huberry::Encryptor.default_options[:algorithm]` which is `aes-256-cbc` by default. You can change the default options
24
+ by overwriting or merging this attribute:
25
+
26
+ Huberry::Encryptor.default_options.merge!(:algorithm => 'bf', :key => 'some default secret key')
27
+
28
+ You can extract the `Encryptor` module out of the `Huberry` namespace if you'd like
29
+
30
+ Encryptor = Huberry::Encryptor
31
+
32
+ Run `openssl list-cipher-commands` in your terminal to view a list all cipher algorithms that are supported on your platform.
33
+
34
+ aes-128-cbc
35
+ aes-128-ecb
36
+ aes-192-cbc
37
+ aes-192-ecb
38
+ aes-256-cbc
39
+ aes-256-ecb
40
+ base64
41
+ bf
42
+ bf-cbc
43
+ bf-cfb
44
+ bf-ecb
45
+ bf-ofb
46
+ cast
47
+ cast-cbc
48
+ cast5-cbc
49
+ cast5-cfb
50
+ cast5-ecb
51
+ cast5-ofb
52
+ des
53
+ des-cbc
54
+ des-cfb
55
+ des-ecb
56
+ des-ede
57
+ des-ede-cbc
58
+ des-ede-cfb
59
+ des-ede-ofb
60
+ des-ede3
61
+ des-ede3-cbc
62
+ des-ede3-cfb
63
+ des-ede3-ofb
64
+ des-ofb
65
+ des3
66
+ desx
67
+ idea
68
+ idea-cbc
69
+ idea-cfb
70
+ idea-ecb
71
+ idea-ofb
72
+ rc2
73
+ rc2-40-cbc
74
+ rc2-64-cbc
75
+ rc2-cbc
76
+ rc2-cfb
77
+ rc2-ecb
78
+ rc2-ofb
79
+ rc4
80
+ rc4-40
81
+
82
+
83
+ Contact
84
+ -------
85
+
86
+ 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,51 @@
1
+ require 'openssl'
2
+
3
+ module Huberry
4
+ module Encryptor
5
+ # The default options to use when calling the <tt>encrypt</tt> and <tt>decrypt</tt> methods
6
+ #
7
+ # Defaults to { :algorithm => '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_options; end
11
+ self.default_options = { :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
+ options = default_options.merge(options)
39
+ cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm])
40
+ cipher.send(cipher_method)
41
+ if options[:iv]
42
+ cipher.key = options[:key]
43
+ cipher.iv = options[:iv]
44
+ else
45
+ cipher.pkcs5_keyivgen(options[:key])
46
+ end
47
+ result = cipher.update(options[:value])
48
+ result << cipher.final
49
+ end
50
+ end
51
+ 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_options[: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_options[: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_options[:algorithm]
37
+ assert_not_equal 'test', original_algorithm
38
+ Huberry::Encryptor.default_options[:algorithm] = 'test'
39
+ assert_equal 'test', Huberry::Encryptor.default_options[:algorithm]
40
+ Huberry::Encryptor.default_options[:algorithm] = original_algorithm
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encryptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Huber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-10 00:00:00 -05: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
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --line-numbers
37
+ - --inline-source
38
+ - --main
39
+ - README.markdown
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: A simple wrapper for the standard ruby OpenSSL library
61
+ test_files:
62
+ - test/encryptor_test.rb