encryptor 1.0.1 → 1.1.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/README.rdoc +90 -0
- data/Rakefile +1 -1
- data/lib/encryptor.rb +46 -47
- data/test/encryptor_test.rb +11 -11
- metadata +6 -6
- data/CHANGELOG +0 -12
- data/README.markdown +0 -86
data/README.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
= Encryptor
|
2
|
+
|
3
|
+
A simple wrapper for the standard ruby OpenSSL library
|
4
|
+
|
5
|
+
Used by http://github.com/shuber/attr_encrypted to easily encrypt/decrypt attributes in any class
|
6
|
+
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
gem install encryptor
|
11
|
+
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
secret_key = Digest::SHA256.hexdigest('a secret key')
|
16
|
+
encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => secret_key) # '������{)��q�ށ�ܣ��q���Au/�ޜP'
|
17
|
+
decrypted_value = Encryptor.decrypt(:value => encrypted_value, :key => secret_key) # 'some string to encrypt'
|
18
|
+
|
19
|
+
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
|
20
|
+
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
|
21
|
+
by overwriting or merging this attribute:
|
22
|
+
|
23
|
+
Encryptor.default_options.merge!(:algorithm => 'bf', :key => 'some default secret key')
|
24
|
+
|
25
|
+
Run <tt>openssl list-cipher-commands</tt> in your terminal to view a list all cipher algorithms that are supported on your platform.
|
26
|
+
|
27
|
+
aes-128-cbc
|
28
|
+
aes-128-ecb
|
29
|
+
aes-192-cbc
|
30
|
+
aes-192-ecb
|
31
|
+
aes-256-cbc
|
32
|
+
aes-256-ecb
|
33
|
+
base64
|
34
|
+
bf
|
35
|
+
bf-cbc
|
36
|
+
bf-cfb
|
37
|
+
bf-ecb
|
38
|
+
bf-ofb
|
39
|
+
cast
|
40
|
+
cast-cbc
|
41
|
+
cast5-cbc
|
42
|
+
cast5-cfb
|
43
|
+
cast5-ecb
|
44
|
+
cast5-ofb
|
45
|
+
des
|
46
|
+
des-cbc
|
47
|
+
des-cfb
|
48
|
+
des-ecb
|
49
|
+
des-ede
|
50
|
+
des-ede-cbc
|
51
|
+
des-ede-cfb
|
52
|
+
des-ede-ofb
|
53
|
+
des-ede3
|
54
|
+
des-ede3-cbc
|
55
|
+
des-ede3-cfb
|
56
|
+
des-ede3-ofb
|
57
|
+
des-ofb
|
58
|
+
des3
|
59
|
+
desx
|
60
|
+
idea
|
61
|
+
idea-cbc
|
62
|
+
idea-cfb
|
63
|
+
idea-ecb
|
64
|
+
idea-ofb
|
65
|
+
rc2
|
66
|
+
rc2-40-cbc
|
67
|
+
rc2-64-cbc
|
68
|
+
rc2-cbc
|
69
|
+
rc2-cfb
|
70
|
+
rc2-ecb
|
71
|
+
rc2-ofb
|
72
|
+
rc4
|
73
|
+
rc4-40
|
74
|
+
|
75
|
+
|
76
|
+
== Note on Patches/Pull Requests
|
77
|
+
|
78
|
+
* Fork the project.
|
79
|
+
* Make your feature addition or bug fix.
|
80
|
+
* Add tests for it. This is important so I don't break it in a
|
81
|
+
future version unintentionally.
|
82
|
+
* Commit, do not mess with rakefile, version, or history.
|
83
|
+
(if you want to have your own version, that is fine but
|
84
|
+
bump version in a commit by itself I can ignore when I pull)
|
85
|
+
* Send me a pull request. Bonus points for topic branches.
|
86
|
+
|
87
|
+
|
88
|
+
== Contact
|
89
|
+
|
90
|
+
Problems, comments, and suggestions all welcome: shuber@huberry.com
|
data/Rakefile
CHANGED
@@ -17,6 +17,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
17
17
|
rdoc.rdoc_dir = 'rdoc'
|
18
18
|
rdoc.title = 'Encryptor'
|
19
19
|
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
-
rdoc.rdoc_files.include('README
|
20
|
+
rdoc.rdoc_files.include('README*')
|
21
21
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
22
|
end
|
data/lib/encryptor.rb
CHANGED
@@ -1,51 +1,50 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
3
|
+
module Encryptor
|
4
|
+
# The default options to use when calling the <tt>encrypt</tt> and <tt>decrypt</tt> methods
|
5
|
+
#
|
6
|
+
# Defaults to { :algorithm => 'aes-256-cbc' }
|
7
|
+
#
|
8
|
+
# Run 'openssl list-cipher-commands' in your terminal to view a list all cipher algorithms that are supported on your platform
|
9
|
+
def self.default_options
|
10
|
+
@default_options ||= { :algorithm => 'aes-256-cbc' }
|
11
|
+
end
|
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
|
50
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
|
51
50
|
end
|
data/test/encryptor_test.rb
CHANGED
@@ -11,33 +11,33 @@ class EncryptorTest < Test::Unit::TestCase
|
|
11
11
|
|
12
12
|
algorithms.each do |algorithm|
|
13
13
|
define_method "test_should_crypt_with_#{algorithm}_algorithm_with_iv" do
|
14
|
-
encrypted_value =
|
14
|
+
encrypted_value = Encryptor.encrypt(:value => original_value, :key => key, :iv => iv, :algorithm => algorithm)
|
15
15
|
assert_not_equal original_value, encrypted_value
|
16
|
-
assert_not_equal
|
17
|
-
assert_equal original_value,
|
16
|
+
assert_not_equal Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm), encrypted_value
|
17
|
+
assert_equal original_value, Encryptor.decrypt(:value => encrypted_value, :key => key, :iv => iv, :algorithm => algorithm)
|
18
18
|
end
|
19
19
|
|
20
20
|
define_method "test_should_crypt_with_#{algorithm}_algorithm_without_iv" do
|
21
|
-
encrypted_value =
|
21
|
+
encrypted_value = Encryptor.encrypt(:value => original_value, :key => key, :algorithm => algorithm)
|
22
22
|
assert_not_equal original_value, encrypted_value
|
23
|
-
assert_equal original_value,
|
23
|
+
assert_equal original_value, Encryptor.decrypt(:value => encrypted_value, :key => key, :algorithm => algorithm)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
define_method 'test_should_have_a_default_algorithm' do
|
28
|
-
assert algorithms.include?(
|
28
|
+
assert algorithms.include?(Encryptor.default_options[:algorithm])
|
29
29
|
end
|
30
30
|
|
31
31
|
define_method 'test_should_use_the_default_algorithm_if_one_is_not_specified' do
|
32
|
-
assert_equal
|
32
|
+
assert_equal Encryptor.encrypt(:value => original_value, :key => key, :algorithm => Encryptor.default_options[:algorithm]), Encryptor.encrypt(:value => original_value, :key => key)
|
33
33
|
end
|
34
34
|
|
35
35
|
def test_should_be_able_to_change_the_default_algorithm
|
36
|
-
original_algorithm =
|
36
|
+
original_algorithm = Encryptor.default_options[:algorithm]
|
37
37
|
assert_not_equal 'test', original_algorithm
|
38
|
-
|
39
|
-
assert_equal 'test',
|
40
|
-
|
38
|
+
Encryptor.default_options[:algorithm] = 'test'
|
39
|
+
assert_equal 'test', Encryptor.default_options[:algorithm]
|
40
|
+
Encryptor.default_options[:algorithm] = original_algorithm
|
41
41
|
end
|
42
42
|
|
43
43
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encryptor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Huber
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-01-28 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,12 +22,12 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
-
- CHANGELOG
|
26
25
|
- lib/encryptor.rb
|
27
26
|
- MIT-LICENSE
|
28
27
|
- Rakefile
|
29
|
-
- README.
|
30
|
-
|
28
|
+
- README.rdoc
|
29
|
+
- test/encryptor_test.rb
|
30
|
+
has_rdoc: true
|
31
31
|
homepage: http://github.com/shuber/encryptor
|
32
32
|
licenses: []
|
33
33
|
|
@@ -36,7 +36,7 @@ rdoc_options:
|
|
36
36
|
- --line-numbers
|
37
37
|
- --inline-source
|
38
38
|
- --main
|
39
|
-
- README.
|
39
|
+
- README.rdoc
|
40
40
|
require_paths:
|
41
41
|
- lib
|
42
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
data/CHANGELOG
DELETED
@@ -1,12 +0,0 @@
|
|
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/README.markdown
DELETED
@@ -1,86 +0,0 @@
|
|
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)
|