encryptor2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ pkg
2
+ rdoc
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 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.
@@ -0,0 +1,115 @@
1
+ Encryptor
2
+ ----------
3
+
4
+ A simple wrapper for the standard ruby OpenSSL library
5
+
6
+ Used by `http://github.com/danpal/attr_encrypted` to easily encrypt/decrypt attributes in any class
7
+
8
+ Installation
9
+ -------------
10
+
11
+ gem install encryptor
12
+
13
+ Usage
14
+ -----------
15
+
16
+ ##Basic
17
+
18
+ Encryptor will use `aes-256-cbc` to encrypt securely. It's highly recommended you use an `iv(initialization vector)` and a `salt`. Encryptor takes this as
19
+ options. If you specify an `:iv` you are required to specify a `:salt` too.
20
+
21
+ The best example is:
22
+
23
+ salt = Time.now.to_i.to_s
24
+ secret_key = "secret"
25
+ iv = (OpenSSL::Cipher::Cipher.new('aes-256-cbc').random_iv
26
+ encrypted_value = Encryptor.encrypt('some string to encrypt', :key => secret_key, :iv => iv, :salt => salt)
27
+ decrypted_value = Encryptor.decrypt(encrypted_value, :key => secret_key, :iv => iv, :salt => salt)
28
+
29
+ The value to encrypt or decrypt may also be passed as the :value option if you'd like.
30
+
31
+ encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key, :iv => iv, :salt => salt)
32
+ decrypted_value = Encryptor.decrypt(:value => encrypted_value, :key => secret_key, :iv => iv, :salt => salt)
33
+
34
+
35
+ **You may also skip the salt and the iv if you want so. Doing so, you are on your own!**
36
+
37
+ encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => 'secret')
38
+ decrypted_value = Encryptor.decrypt(:value => encrypted_value, :key => 'secret')
39
+
40
+
41
+ You may also pass the :algorithm< options but they are not required.
42
+
43
+ Encryptor.default_options.merge!(:algorithm => 'aes-128-cbc', :key => 'some default secret key', :iv => iv, :salt => salt)
44
+
45
+
46
+ === Strings
47
+
48
+ Encryptor adds `encrypt` and `decrypt` methods to String objects for your convenience. These two methods accept the same arguments as the associated ones in the Encryptor module. They're nice when you set the default options in the Encryptor.default_options attribute. For example:
49
+
50
+ Encryptor.default_options.merge!(:key => 'some default secret key', :iv => iv, :salt => salt)
51
+ credit_card = 'xxxx xxxx xxxx 1234'
52
+ encrypted_credit_card = credit_card.encrypt
53
+
54
+ There's also encrypt! and decrypt! methods that replace the contents of a string with the encrypted or decrypted version of itself.
55
+
56
+ === Algorithms
57
+
58
+ Run openssl list-cipher-commands in your terminal to view a list of all cipher algorithms that are supported on your platform.
59
+
60
+ aes-128-cbc
61
+ aes-128-ecb
62
+ aes-192-cbc
63
+ aes-192-ecb
64
+ aes-256-cbc
65
+ aes-256-ecb
66
+ bf
67
+ bf-cbc
68
+ bf-cfb
69
+ bf-ecb
70
+ bf-ofb
71
+ cast
72
+ cast-cbc
73
+ cast5-cbc
74
+ cast5-cfb
75
+ cast5-ecb
76
+ cast5-ofb
77
+ des
78
+ des-cbc
79
+ des-cfb
80
+ des-ecb
81
+ des-ede
82
+ des-ede-cbc
83
+ des-ede-cfb
84
+ des-ede-ofb
85
+ des-ede3
86
+ des-ede3-cbc
87
+ des-ede3-cfb
88
+ des-ede3-ofb
89
+ des-ofb
90
+ des3
91
+ desx
92
+ idea
93
+ idea-cbc
94
+ idea-cfb
95
+ idea-ecb
96
+ idea-ofb
97
+ rc2
98
+ rc2-40-cbc
99
+ rc2-64-cbc
100
+ rc2-cbc
101
+ rc2-cfb
102
+ rc2-ecb
103
+ rc2-ofb
104
+ rc4
105
+ rc4-40
106
+
107
+
108
+ Note on Patches/Pull Requests
109
+ ------------------------------
110
+
111
+ * Fork the project.
112
+ * Make your feature addition or bug fix.
113
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
114
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
115
+ * Send me a pull request. Bonus points for topic branches.
@@ -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*')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib/', __FILE__)
4
+ $:.unshift lib unless $:.include?(lib)
5
+
6
+ require 'encryptor/version'
7
+ require 'date'
8
+
9
+ Gem::Specification.new do |s|
10
+ s.name = 'encryptor2'
11
+ s.version = Encryptor::Version
12
+ s.date = Date.today
13
+ s.platform = Gem::Platform::RUBY
14
+
15
+ s.summary = 'A simple wrapper for the standard ruby OpenSSL library'
16
+ s.description = 'A simple wrapper for the standard ruby OpenSSL library to encrypt and decrypt strings'
17
+
18
+ s.author = 'Daniel Palacio'
19
+ s.email = 'danpal@gmail.com'
20
+ s.homepage = 'http://github.com/danpal/encryptor'
21
+
22
+ s.require_paths = ['lib']
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+
27
+ end
@@ -0,0 +1,65 @@
1
+ require 'openssl'
2
+ require 'encryptor/string'
3
+
4
+ String.send(:include, Encryptor::String)
5
+
6
+ # A simple wrapper for the standard OpenSSL library
7
+ module Encryptor
8
+ autoload :Version, 'encryptor/version'
9
+
10
+ extend self
11
+
12
+ # The default options to use when calling the <tt>encrypt</tt> and <tt>decrypt</tt> methods
13
+ #
14
+ # Defaults to { :algorithm => 'aes-256-cbc' }
15
+ #
16
+ # Run 'openssl list-cipher-commands' in your terminal to view a list all cipher algorithms that are supported on your platform
17
+ def default_options
18
+ @default_options ||= { :algorithm => 'aes-256-cbc' }
19
+ end
20
+
21
+ # Encrypts a <tt>:value</tt> with a specified <tt>:key</tt>
22
+ #
23
+ # Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
24
+ #
25
+ # Example
26
+ #
27
+ # encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
28
+ # # or
29
+ # encrypted_value = Encryptor.encrypt('some string to encrypt', :key => 'some secret key')
30
+ def encrypt(*args, &block)
31
+ crypt :encrypt, *args, &block
32
+ end
33
+
34
+ # Decrypts a <tt>:value</tt> with a specified <tt>:key</tt>
35
+ #
36
+ # Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
37
+ #
38
+ # Example
39
+ #
40
+ # decrypted_value = Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
41
+ # # or
42
+ # decrypted_value = Encryptor.decrypt('some encrypted string', :key => 'some secret key')
43
+ def decrypt(*args, &block)
44
+ crypt :decrypt, *args, &block
45
+ end
46
+
47
+ protected
48
+
49
+ def crypt(cipher_method, *args) #:nodoc:
50
+ options = default_options.merge(:value => args.first).merge(args.last.is_a?(Hash) ? args.last : {})
51
+ raise ArgumentError.new('must specify a :key') if options[:key].to_s.empty?
52
+ cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm])
53
+ cipher.send(cipher_method)
54
+ if options[:iv]
55
+ raise ArgumentError.new('you must specify a :salt') if options[:salt].nil?
56
+ cipher.iv = options[:iv]
57
+ cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(options[:key], options[:salt], 2000, cipher.key_len)
58
+ else
59
+ cipher.pkcs5_keyivgen(options[:key])
60
+ end
61
+ yield cipher, options if block_given?
62
+ result = cipher.update(options[:value])
63
+ result << cipher.final
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ module Encryptor
2
+ # Adds <tt>encrypt</tt> and <tt>decrypt</tt> methods to strings
3
+ module String
4
+ # Returns a new string containing the encrypted version of itself
5
+ def encrypt(options = {})
6
+ Encryptor.encrypt(options.merge(:value => self))
7
+ end
8
+
9
+ # Replaces the contents of a string with the encrypted version of itself
10
+ def encrypt!(options ={})
11
+ replace encrypt(options)
12
+ end
13
+
14
+ # Returns a new string containing the decrypted version of itself
15
+ def decrypt(options = {})
16
+ Encryptor.decrypt(options.merge(:value => self))
17
+ end
18
+
19
+ # Replaces the contents of a string with the decrypted version of itself
20
+ def decrypt!(options ={})
21
+ replace decrypt(options)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ module Encryptor
2
+ # Contains information about this gem's version
3
+ module Version
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ PATCH = 0
7
+
8
+ # Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
9
+ #
10
+ # Example
11
+ #
12
+ # Version.to_s # '1.0.2'
13
+ def self.to_s
14
+ [MAJOR, MINOR, PATCH].join('.')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class EncryptorTest < Test::Unit::TestCase
4
+
5
+ algorithms = %x(openssl list-cipher-commands).split
6
+ key = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
7
+ iv = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
8
+ salt = Time.now.to_i.to_s
9
+ original_value = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
10
+
11
+ algorithms.reject { |algorithm| algorithm == 'base64' }.each do |algorithm|
12
+ encrypted_value_with_iv = Encryptor.encrypt(:value => original_value, :key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
13
+ encrypted_value_without_iv = Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm)
14
+
15
+ define_method "test_should_crypt_with_the_#{algorithm}_algorithm_with_iv" do
16
+ assert_not_equal original_value, encrypted_value_with_iv
17
+ assert_not_equal encrypted_value_without_iv, encrypted_value_with_iv
18
+ assert_equal original_value, Encryptor.decrypt(:value => encrypted_value_with_iv, :key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
19
+ end
20
+
21
+ define_method "test_should_crypt_with_the_#{algorithm}_algorithm_without_iv" do
22
+ assert_not_equal original_value, encrypted_value_without_iv
23
+ assert_equal original_value, Encryptor.decrypt(:value => encrypted_value_without_iv, :key => key, :algorithm => algorithm)
24
+ end
25
+
26
+ define_method "test_should_encrypt_with_the_#{algorithm}_algorithm_with_iv_with_the_first_arg_as_the_value" do
27
+ assert_equal encrypted_value_with_iv, Encryptor.encrypt(original_value, :key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
28
+ end
29
+
30
+ define_method "test_should_encrypt_with_the_#{algorithm}_algorithm_without_iv_with_the_first_arg_as_the_value" do
31
+ assert_equal encrypted_value_without_iv, Encryptor.encrypt(original_value, :key => key, :algorithm => algorithm)
32
+ end
33
+
34
+ define_method "test_should_decrypt_with_the_#{algorithm}_algorithm_with_iv_with_the_first_arg_as_the_value" do
35
+ assert_equal original_value, Encryptor.decrypt(encrypted_value_with_iv, :key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
36
+ end
37
+
38
+ define_method "test_should_decrypt_with_the_#{algorithm}_algorithm_without_iv_with_the_first_arg_as_the_value" do
39
+ assert_equal original_value, Encryptor.decrypt(encrypted_value_without_iv, :key => key, :algorithm => algorithm)
40
+ end
41
+
42
+ define_method "test_should_call_encrypt_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
43
+ assert_equal encrypted_value_with_iv, original_value.encrypt(:key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
44
+ end
45
+
46
+ define_method "test_should_call_encrypt_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
47
+ assert_equal encrypted_value_without_iv, original_value.encrypt(:key => key, :algorithm => algorithm)
48
+ end
49
+
50
+ define_method "test_should_call_decrypt_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
51
+ assert_equal original_value, encrypted_value_with_iv.decrypt(:key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
52
+ end
53
+
54
+ define_method "test_should_call_decrypt_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
55
+ assert_equal original_value, encrypted_value_without_iv.decrypt(:key => key, :algorithm => algorithm)
56
+ end
57
+
58
+ define_method "test_string_encrypt!_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
59
+ original_value_dup = original_value.dup
60
+ original_value_dup.encrypt!(:key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
61
+ assert_equal original_value.encrypt(:key => key, :iv => iv, :salt => salt, :algorithm => algorithm), original_value_dup
62
+ end
63
+
64
+ define_method "test_string_encrypt!_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
65
+ original_value_dup = original_value.dup
66
+ original_value_dup.encrypt!(:key => key, :algorithm => algorithm)
67
+ assert_equal original_value.encrypt(:key => key, :algorithm => algorithm), original_value_dup
68
+ end
69
+
70
+ define_method "test_string_decrypt!_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
71
+ encrypted_value_with_iv_dup = encrypted_value_with_iv.dup
72
+ encrypted_value_with_iv_dup.decrypt!(:key => key, :iv => iv, :salt => salt, :algorithm => algorithm)
73
+ assert_equal original_value, encrypted_value_with_iv_dup
74
+ end
75
+
76
+ define_method "test_string_decrypt!_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
77
+ encrypted_value_without_iv_dup = encrypted_value_without_iv.dup
78
+ encrypted_value_without_iv_dup.decrypt!(:key => key, :algorithm => algorithm)
79
+ assert_equal original_value, encrypted_value_without_iv_dup
80
+ end
81
+ end
82
+
83
+ define_method 'test_should_use_the_default_algorithm_if_one_is_not_specified' do
84
+ assert_equal Encryptor.encrypt(:value => original_value, :key => key, :algorithm => Encryptor.default_options[:algorithm]), Encryptor.encrypt(:value => original_value, :key => key)
85
+ end
86
+
87
+ def test_should_have_a_default_algorithm
88
+ assert !Encryptor.default_options[:algorithm].nil?
89
+ assert !Encryptor.default_options[:algorithm].empty?
90
+ end
91
+
92
+ def test_should_raise_argument_error_if_key_is_not_specified
93
+ assert_raises(ArgumentError) { Encryptor.encrypt('some value') }
94
+ assert_raises(ArgumentError) { Encryptor.decrypt('some encrypted string') }
95
+ assert_raises(ArgumentError) { Encryptor.encrypt('some value', :key => '') }
96
+ assert_raises(ArgumentError) { Encryptor.decrypt('some encrypted string', :key => '') }
97
+ end
98
+
99
+ def test_should_yield_block_with_cipher_and_options
100
+ called = false
101
+ Encryptor.encrypt('some value', :key => 'some key') { |cipher, options| called = true }
102
+ assert called
103
+ end
104
+
105
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ require 'digest/sha2'
3
+
4
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $:.unshift(File.dirname(__FILE__))
6
+ require 'encryptor'
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encryptor2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Palacio
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-16 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A simple wrapper for the standard ruby OpenSSL library to encrypt and
15
+ decrypt strings
16
+ email: danpal@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - MIT-LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - encryptor.gemspec
26
+ - lib/encryptor.rb
27
+ - lib/encryptor/string.rb
28
+ - lib/encryptor/version.rb
29
+ - test/encryptor_test.rb
30
+ - test/test_helper.rb
31
+ homepage: http://github.com/danpal/encryptor
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.6
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: A simple wrapper for the standard ruby OpenSSL library
55
+ test_files:
56
+ - test/encryptor_test.rb
57
+ - test/test_helper.rb