encryptor 1.1.1 → 1.1.2
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/MIT-LICENSE +1 -1
- data/README.rdoc +10 -18
- data/Rakefile +3 -3
- data/lib/encryptor.rb +15 -11
- data/lib/encryptor/string.rb +4 -4
- data/lib/encryptor/version.rb +17 -0
- data/test/encryptor_test.rb +19 -21
- data/test/test_helper.rb +6 -0
- metadata +22 -7
data/MIT-LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -15,15 +15,15 @@ Used by http://github.com/shuber/attr_encrypted to easily encrypt/decrypt attrib
|
|
15
15
|
|
16
16
|
=== Basic
|
17
17
|
|
18
|
-
encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
|
19
|
-
decrypted_value = Encryptor.decrypt(:value => encrypted_value, :key => secret_key) # 'some string to encrypt'
|
20
|
-
|
21
|
-
The value to encrypt or decrypt may also be passed as the first argument if you'd like.
|
22
|
-
|
23
18
|
secret_key = Digest::SHA256.hexdigest('a secret key')
|
24
19
|
encrypted_value = Encryptor.encrypt('some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
|
25
20
|
decrypted_value = Encryptor.decrypt(encrypted_value, :key => secret_key) # 'some string to encrypt'
|
26
21
|
|
22
|
+
The value to encrypt or decrypt may also be passed as the <tt>:value</tt> option if you'd like.
|
23
|
+
|
24
|
+
encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
|
25
|
+
decrypted_value = Encryptor.decrypt(:value => encrypted_value, :key => secret_key) # 'some string to encrypt'
|
26
|
+
|
27
27
|
You may also pass the <tt>:iv</tt> and <tt>:algorithm</tt> options but they are not required. If an algorithm is not specified, the Encryptor uses
|
28
28
|
the algorithm found at <tt>Encryptor.default_options[:algorithm]</tt> which is <tt>aes-256-cbc</tt> by default. You can change the default options
|
29
29
|
by overwriting or merging this attribute:
|
@@ -33,7 +33,7 @@ by overwriting or merging this attribute:
|
|
33
33
|
|
34
34
|
=== Strings
|
35
35
|
|
36
|
-
<tt>Encryptor</tt> adds <tt>encrypt</tt> and <tt>decrypt</tt> methods to <tt>String</tt> objects for your convenience. These two methods accept the same arguments as the associated ones in the <tt>Encryptor</tt> module.
|
36
|
+
<tt>Encryptor</tt> adds <tt>encrypt</tt> and <tt>decrypt</tt> methods to <tt>String</tt> objects for your convenience. These two methods accept the same arguments as the associated ones in the <tt>Encryptor</tt> module. They're nice when you set the default options in the <tt>Encryptor.default_options</tt> attribute. For example:
|
37
37
|
|
38
38
|
Encryptor.default_options.merge!(:key => 'some default secret key')
|
39
39
|
credit_card = 'xxxx xxxx xxxx 1234'
|
@@ -44,7 +44,7 @@ There's also <tt>encrypt!</tt> and <tt>decrypt!</tt> methods that replace the co
|
|
44
44
|
|
45
45
|
=== Algorithms
|
46
46
|
|
47
|
-
Run <tt>openssl list-cipher-commands</tt> in your terminal to view a list all cipher algorithms that are supported on your platform.
|
47
|
+
Run <tt>openssl list-cipher-commands</tt> in your terminal to view a list of all cipher algorithms that are supported on your platform.
|
48
48
|
|
49
49
|
aes-128-cbc
|
50
50
|
aes-128-ecb
|
@@ -98,14 +98,6 @@ Run <tt>openssl list-cipher-commands</tt> in your terminal to view a list all ci
|
|
98
98
|
|
99
99
|
* Fork the project.
|
100
100
|
* Make your feature addition or bug fix.
|
101
|
-
* Add tests for it. This is important so I don't break it in a
|
102
|
-
|
103
|
-
*
|
104
|
-
(if you want to have your own version, that is fine but
|
105
|
-
bump version in a commit by itself I can ignore when I pull)
|
106
|
-
* Send me a pull request. Bonus points for topic branches.
|
107
|
-
|
108
|
-
|
109
|
-
== Contact
|
110
|
-
|
111
|
-
Problems, comments, and suggestions all welcome: shuber@huberry.com
|
101
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
102
|
+
* 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)
|
103
|
+
* Send me a pull request. Bonus points for topic branches.
|
data/Rakefile
CHANGED
@@ -2,17 +2,17 @@ require 'rake'
|
|
2
2
|
require 'rake/testtask'
|
3
3
|
require 'rake/rdoctask'
|
4
4
|
|
5
|
-
desc 'Default: run unit tests
|
5
|
+
desc 'Default: run unit tests'
|
6
6
|
task :default => :test
|
7
7
|
|
8
|
-
desc 'Test the encryptor gem
|
8
|
+
desc 'Test the encryptor gem'
|
9
9
|
Rake::TestTask.new(:test) do |t|
|
10
10
|
t.libs << 'lib'
|
11
11
|
t.pattern = 'test/**/*_test.rb'
|
12
12
|
t.verbose = true
|
13
13
|
end
|
14
14
|
|
15
|
-
desc 'Generate documentation for the encryptor gem
|
15
|
+
desc 'Generate documentation for the encryptor gem'
|
16
16
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
17
|
rdoc.rdoc_dir = 'rdoc'
|
18
18
|
rdoc.title = 'Encryptor'
|
data/lib/encryptor.rb
CHANGED
@@ -1,17 +1,23 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
require 'encryptor/string'
|
3
3
|
|
4
|
+
String.send(:include, Encryptor::String)
|
5
|
+
|
4
6
|
# A simple wrapper for the standard OpenSSL library
|
5
7
|
module Encryptor
|
8
|
+
autoload :Version, 'encryptor/version'
|
9
|
+
|
10
|
+
extend self
|
11
|
+
|
6
12
|
# The default options to use when calling the <tt>encrypt</tt> and <tt>decrypt</tt> methods
|
7
13
|
#
|
8
14
|
# Defaults to { :algorithm => 'aes-256-cbc' }
|
9
15
|
#
|
10
16
|
# Run 'openssl list-cipher-commands' in your terminal to view a list all cipher algorithms that are supported on your platform
|
11
|
-
def
|
17
|
+
def default_options
|
12
18
|
@default_options ||= { :algorithm => 'aes-256-cbc' }
|
13
19
|
end
|
14
|
-
|
20
|
+
|
15
21
|
# Encrypts a <tt>:value</tt> with a specified <tt>:key</tt>
|
16
22
|
#
|
17
23
|
# Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
|
@@ -21,10 +27,10 @@ module Encryptor
|
|
21
27
|
# encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
|
22
28
|
# # or
|
23
29
|
# encrypted_value = Encryptor.encrypt('some string to encrypt', :key => 'some secret key')
|
24
|
-
def
|
30
|
+
def encrypt(*args)
|
25
31
|
crypt :encrypt, *args
|
26
32
|
end
|
27
|
-
|
33
|
+
|
28
34
|
# Decrypts a <tt>:value</tt> with a specified <tt>:key</tt>
|
29
35
|
#
|
30
36
|
# Optionally accepts <tt>:iv</tt> and <tt>:algorithm</tt> options
|
@@ -34,13 +40,13 @@ module Encryptor
|
|
34
40
|
# decrypted_value = Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
|
35
41
|
# # or
|
36
42
|
# decrypted_value = Encryptor.decrypt('some encrypted string', :key => 'some secret key')
|
37
|
-
def
|
43
|
+
def decrypt(*args)
|
38
44
|
crypt :decrypt, *args
|
39
45
|
end
|
40
|
-
|
46
|
+
|
41
47
|
protected
|
42
|
-
|
43
|
-
def
|
48
|
+
|
49
|
+
def crypt(cipher_method, *args) #:nodoc:
|
44
50
|
options = default_options.merge(:value => args.first).merge(args.last.is_a?(Hash) ? args.last : {})
|
45
51
|
cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm])
|
46
52
|
cipher.send(cipher_method)
|
@@ -53,6 +59,4 @@ module Encryptor
|
|
53
59
|
result = cipher.update(options[:value])
|
54
60
|
result << cipher.final
|
55
61
|
end
|
56
|
-
end
|
57
|
-
|
58
|
-
String.send :include, Encryptor::String
|
62
|
+
end
|
data/lib/encryptor/string.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
module Encryptor
|
1
|
+
module Encryptor
|
2
2
|
# Adds <tt>encrypt</tt> and <tt>decrypt</tt> methods to strings
|
3
3
|
module String
|
4
4
|
# Returns a new string containing the encrypted version of itself
|
5
5
|
def encrypt(options = {})
|
6
6
|
Encryptor.encrypt(options.merge(:value => self))
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
# Replaces the contents of a string with the encrypted version of itself
|
10
10
|
def encrypt!(options ={})
|
11
11
|
replace encrypt(options)
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
# Returns a new string containing the decrypted version of itself
|
15
15
|
def decrypt(options = {})
|
16
16
|
Encryptor.decrypt(options.merge(:value => self))
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
# Replaces the contents of a string with the decrypted version of itself
|
20
20
|
def decrypt!(options ={})
|
21
21
|
replace decrypt(options)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Encryptor
|
2
|
+
# Contains information about this gem's version
|
3
|
+
module Version
|
4
|
+
MAJOR = 1
|
5
|
+
MINOR = 1
|
6
|
+
PATCH = 2
|
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
|
data/test/encryptor_test.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
require '
|
2
|
-
require 'digest/sha2'
|
3
|
-
require File.dirname(__FILE__) + '/../lib/encryptor'
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
4
2
|
|
5
3
|
class EncryptorTest < Test::Unit::TestCase
|
6
|
-
|
4
|
+
|
7
5
|
algorithms = %x(openssl list-cipher-commands).split
|
8
6
|
key = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
|
9
7
|
iv = Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join)
|
@@ -12,82 +10,82 @@ class EncryptorTest < Test::Unit::TestCase
|
|
12
10
|
algorithms.reject { |algorithm| algorithm == 'base64' }.each do |algorithm|
|
13
11
|
encrypted_value_with_iv = Encryptor.encrypt(:value => original_value, :key => key, :iv => iv, :algorithm => algorithm)
|
14
12
|
encrypted_value_without_iv = Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm)
|
15
|
-
|
13
|
+
|
16
14
|
define_method "test_should_crypt_with_the_#{algorithm}_algorithm_with_iv" do
|
17
15
|
assert_not_equal original_value, encrypted_value_with_iv
|
18
16
|
assert_not_equal encrypted_value_without_iv, encrypted_value_with_iv
|
19
17
|
assert_equal original_value, Encryptor.decrypt(:value => encrypted_value_with_iv, :key => key, :iv => iv, :algorithm => algorithm)
|
20
18
|
end
|
21
|
-
|
19
|
+
|
22
20
|
define_method "test_should_crypt_with_the_#{algorithm}_algorithm_without_iv" do
|
23
21
|
assert_not_equal original_value, encrypted_value_without_iv
|
24
22
|
assert_equal original_value, Encryptor.decrypt(:value => encrypted_value_without_iv, :key => key, :algorithm => algorithm)
|
25
23
|
end
|
26
|
-
|
24
|
+
|
27
25
|
define_method "test_should_encrypt_with_the_#{algorithm}_algorithm_with_iv_with_the_first_arg_as_the_value" do
|
28
26
|
assert_equal encrypted_value_with_iv, Encryptor.encrypt(original_value, :key => key, :iv => iv, :algorithm => algorithm)
|
29
27
|
end
|
30
|
-
|
28
|
+
|
31
29
|
define_method "test_should_encrypt_with_the_#{algorithm}_algorithm_without_iv_with_the_first_arg_as_the_value" do
|
32
30
|
assert_equal encrypted_value_without_iv, Encryptor.encrypt(original_value, :key => key, :algorithm => algorithm)
|
33
31
|
end
|
34
|
-
|
32
|
+
|
35
33
|
define_method "test_should_decrypt_with_the_#{algorithm}_algorithm_with_iv_with_the_first_arg_as_the_value" do
|
36
34
|
assert_equal original_value, Encryptor.decrypt(encrypted_value_with_iv, :key => key, :iv => iv, :algorithm => algorithm)
|
37
35
|
end
|
38
|
-
|
36
|
+
|
39
37
|
define_method "test_should_decrypt_with_the_#{algorithm}_algorithm_without_iv_with_the_first_arg_as_the_value" do
|
40
38
|
assert_equal original_value, Encryptor.decrypt(encrypted_value_without_iv, :key => key, :algorithm => algorithm)
|
41
39
|
end
|
42
|
-
|
40
|
+
|
43
41
|
define_method "test_should_call_encrypt_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
|
44
42
|
assert_equal encrypted_value_with_iv, original_value.encrypt(:key => key, :iv => iv, :algorithm => algorithm)
|
45
43
|
end
|
46
|
-
|
44
|
+
|
47
45
|
define_method "test_should_call_encrypt_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
|
48
46
|
assert_equal encrypted_value_without_iv, original_value.encrypt(:key => key, :algorithm => algorithm)
|
49
47
|
end
|
50
|
-
|
48
|
+
|
51
49
|
define_method "test_should_call_decrypt_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
|
52
50
|
assert_equal original_value, encrypted_value_with_iv.decrypt(:key => key, :iv => iv, :algorithm => algorithm)
|
53
51
|
end
|
54
|
-
|
52
|
+
|
55
53
|
define_method "test_should_call_decrypt_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
|
56
54
|
assert_equal original_value, encrypted_value_without_iv.decrypt(:key => key, :algorithm => algorithm)
|
57
55
|
end
|
58
|
-
|
56
|
+
|
59
57
|
define_method "test_string_encrypt!_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
|
60
58
|
original_value_dup = original_value.dup
|
61
59
|
original_value_dup.encrypt!(:key => key, :iv => iv, :algorithm => algorithm)
|
62
60
|
assert_equal original_value.encrypt(:key => key, :iv => iv, :algorithm => algorithm), original_value_dup
|
63
61
|
end
|
64
|
-
|
62
|
+
|
65
63
|
define_method "test_string_encrypt!_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
|
66
64
|
original_value_dup = original_value.dup
|
67
65
|
original_value_dup.encrypt!(:key => key, :algorithm => algorithm)
|
68
66
|
assert_equal original_value.encrypt(:key => key, :algorithm => algorithm), original_value_dup
|
69
67
|
end
|
70
|
-
|
68
|
+
|
71
69
|
define_method "test_string_decrypt!_on_a_string_with_the_#{algorithm}_algorithm_with_iv" do
|
72
70
|
encrypted_value_with_iv_dup = encrypted_value_with_iv.dup
|
73
71
|
encrypted_value_with_iv_dup.decrypt!(:key => key, :iv => iv, :algorithm => algorithm)
|
74
72
|
assert_equal original_value, encrypted_value_with_iv_dup
|
75
73
|
end
|
76
|
-
|
74
|
+
|
77
75
|
define_method "test_string_decrypt!_on_a_string_with_the_#{algorithm}_algorithm_without_iv" do
|
78
76
|
encrypted_value_without_iv_dup = encrypted_value_without_iv.dup
|
79
77
|
encrypted_value_without_iv_dup.decrypt!(:key => key, :algorithm => algorithm)
|
80
78
|
assert_equal original_value, encrypted_value_without_iv_dup
|
81
79
|
end
|
82
80
|
end
|
83
|
-
|
81
|
+
|
84
82
|
define_method 'test_should_use_the_default_algorithm_if_one_is_not_specified' do
|
85
83
|
assert_equal Encryptor.encrypt(:value => original_value, :key => key, :algorithm => Encryptor.default_options[:algorithm]), Encryptor.encrypt(:value => original_value, :key => key)
|
86
84
|
end
|
87
|
-
|
85
|
+
|
88
86
|
def test_should_have_a_default_algorithm
|
89
87
|
assert !Encryptor.default_options[:algorithm].nil?
|
90
88
|
assert !Encryptor.default_options[:algorithm].empty?
|
91
89
|
end
|
92
|
-
|
90
|
+
|
93
91
|
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encryptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 1.1.2
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Sean Huber
|
@@ -9,11 +15,11 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2011-04-02 00:00:00 -07:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
16
|
-
description: A simple wrapper for the standard ruby OpenSSL library
|
22
|
+
description: A simple wrapper for the standard ruby OpenSSL library to encrypt and decrypt strings
|
17
23
|
email: shuber@huberry.com
|
18
24
|
executables: []
|
19
25
|
|
@@ -22,12 +28,14 @@ extensions: []
|
|
22
28
|
extra_rdoc_files: []
|
23
29
|
|
24
30
|
files:
|
25
|
-
- lib/encryptor.rb
|
26
31
|
- lib/encryptor/string.rb
|
32
|
+
- lib/encryptor/version.rb
|
33
|
+
- lib/encryptor.rb
|
27
34
|
- MIT-LICENSE
|
28
35
|
- Rakefile
|
29
36
|
- README.rdoc
|
30
37
|
- test/encryptor_test.rb
|
38
|
+
- test/test_helper.rb
|
31
39
|
has_rdoc: true
|
32
40
|
homepage: http://github.com/shuber/encryptor
|
33
41
|
licenses: []
|
@@ -41,23 +49,30 @@ rdoc_options:
|
|
41
49
|
require_paths:
|
42
50
|
- lib
|
43
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
44
53
|
requirements:
|
45
54
|
- - ">="
|
46
55
|
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
47
59
|
version: "0"
|
48
|
-
version:
|
49
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
50
62
|
requirements:
|
51
63
|
- - ">="
|
52
64
|
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
53
68
|
version: "0"
|
54
|
-
version:
|
55
69
|
requirements: []
|
56
70
|
|
57
71
|
rubyforge_project:
|
58
|
-
rubygems_version: 1.
|
72
|
+
rubygems_version: 1.5.0
|
59
73
|
signing_key:
|
60
74
|
specification_version: 3
|
61
75
|
summary: A simple wrapper for the standard ruby OpenSSL library
|
62
76
|
test_files:
|
63
77
|
- test/encryptor_test.rb
|
78
|
+
- test/test_helper.rb
|