easy_encryption 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -31
- data/easy_encryption.gemspec +5 -6
- data/lib/easy_encryption.rb +6 -33
- data/lib/easy_encryption/class_methods.rb +7 -4
- data/lib/easy_encryption/instance_methods.rb +9 -25
- data/lib/easy_encryption/version.rb +1 -1
- data/spec/easy_encryption_spec.rb +4 -16
- data/spec/spec_helper.rb +0 -1
- data/spec/string_spec.rb +11 -50
- metadata +24 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46800c74fe61e6860297f1b472052ce3e59d6674
|
4
|
+
data.tar.gz: b0f0b5ae1852a99303661812061d66b150c1a547
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1fb294c61bf6f67cf55e1681f81319d72859408167ce17fba76006fe87bc71f8c2477de23a4876a15e64c3176f56dc8c96ad6b69da521aa976e41dfe3b6a4ca
|
7
|
+
data.tar.gz: 7c294485ea9181fcfffbf927f691be30e43509b4e824197fb75587b6e8e1c85b2838516f2c145797a2dd6b5db07bddb4a85759c8d08dbd3a658ecb356a93cc30
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# EasyEncryption
|
2
2
|
|
3
|
-
Easy Encryption provides a simple means for encrypting and decrypting strings.
|
3
|
+
Easy Encryption provides a simple means for encrypting and decrypting strings. It utilizes the RbNaCl gem's simple box under the hood to do the actual encryption work (https://github.com/cryptosphere/rbnacl).
|
4
|
+
|
5
|
+
## Important
|
6
|
+
|
7
|
+
libsodium is required for the EasyEncryption gem to function. To install libsodium please refer to https://github.com/jedisct1/libsodium.
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -16,20 +20,18 @@ Or install it yourself as:
|
|
16
20
|
|
17
21
|
$ gem install easy_encryption
|
18
22
|
|
19
|
-
Finally, call setup on EasyEncryption
|
23
|
+
Finally, call setup on EasyEncryption with a password:
|
20
24
|
```ruby
|
21
|
-
EasyEncryption.setup
|
25
|
+
EasyEncryption.setup('secret')
|
22
26
|
```
|
23
27
|
|
24
|
-
In a Rails app you will need to set the default
|
28
|
+
In a Rails app you will need to set the default password inside of your app's ::Application.configure block:
|
25
29
|
```ruby
|
26
30
|
config.after_initialize do
|
27
|
-
EasyEncryption.setup
|
31
|
+
EasyEncryption.setup('secret')
|
28
32
|
end
|
29
33
|
```
|
30
34
|
|
31
|
-
To enable the gem for your Rails tests, set the default cipher key inside of `test_helper.rb` as in the first example.
|
32
|
-
|
33
35
|
## Usage
|
34
36
|
|
35
37
|
EasyEncryption provides two additional methods that can be called on any string.
|
@@ -43,27 +45,3 @@ encrypted_data = 'foo'.encrypt
|
|
43
45
|
```ruby
|
44
46
|
decrypted_data = encrypted_data.decrypt
|
45
47
|
```
|
46
|
-
|
47
|
-
For special cases where a different cipher key needs to be used, it can be passed directly:
|
48
|
-
```ruby
|
49
|
-
encrypted_data = 'foo'.encrypt 'secret'
|
50
|
-
decrypted_data = 'encrypted data'.decrypt 'secret'
|
51
|
-
```
|
52
|
-
|
53
|
-
Alternatively:
|
54
|
-
```ruby
|
55
|
-
string = 'foo'
|
56
|
-
string.cipher_key = 'secret'
|
57
|
-
encrypted_data = string.encrypt
|
58
|
-
|
59
|
-
encrypted_data.cipher_key = 'secret'
|
60
|
-
decrypted_data = encrypted_data.decrypt
|
61
|
-
```
|
62
|
-
|
63
|
-
## Contributing
|
64
|
-
|
65
|
-
1. Fork it
|
66
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
-
5. Create new Pull Request
|
data/easy_encryption.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Adam Ervans"]
|
10
10
|
spec.email = ["aervans@gmail.com"]
|
11
11
|
spec.description = %q{Check the github page for a detailed description}
|
12
|
-
spec.summary = %q{Adds easy to use encrypt and decrypt methods to
|
13
|
-
spec.homepage = "https://github.com/
|
12
|
+
spec.summary = %q{Adds easy to use encrypt and decrypt methods to string instances.}
|
13
|
+
spec.homepage = "https://github.com/aervans/easyencryption"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -19,8 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
-
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "rspec"
|
24
|
-
spec.
|
25
|
-
spec.add_dependency "gibberish"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.1.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
24
|
+
spec.add_dependency "rbnacl", "~> 3.0.0"
|
26
25
|
end
|
data/lib/easy_encryption.rb
CHANGED
@@ -1,43 +1,16 @@
|
|
1
1
|
require "easy_encryption/version"
|
2
2
|
require "easy_encryption/instance_methods"
|
3
3
|
require "easy_encryption/class_methods"
|
4
|
-
require "
|
4
|
+
require "rbnacl"
|
5
5
|
|
6
6
|
module EasyEncryption
|
7
7
|
|
8
|
-
# sets up EasyEncryption
|
9
|
-
|
10
|
-
|
8
|
+
# sets up EasyEncryption.
|
9
|
+
#
|
10
|
+
# EasyEncryption.setup('secret')
|
11
|
+
def self.setup(key)
|
11
12
|
String.send :include, InstanceMethods
|
12
13
|
String.send :extend, ClassMethods
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
# getter for the default cipher key
|
17
|
-
def self.cipher_key
|
18
|
-
@cipher_key
|
19
|
-
end
|
20
|
-
|
21
|
-
# setter to change the default cipher key
|
22
|
-
def self.cipher_key=(cipher_key)
|
23
|
-
@cipher_key = cipher_key
|
24
|
-
end
|
25
|
-
|
26
|
-
# lambda for creating Gibberish ciphers
|
27
|
-
CREATE_CIPHER = lambda do |cipher_key|
|
28
|
-
|
29
|
-
if cipher_key == @last_cipher_key
|
30
|
-
# no cipher has been created or cipher_key is the same
|
31
|
-
@cipher = Gibberish::AES.new cipher_key
|
32
|
-
else
|
33
|
-
# cipher_key was changed
|
34
|
-
@cipher = Gibberish::AES.new cipher_key
|
35
|
-
end
|
36
|
-
|
37
|
-
# remember the cipher key to determine if a new cipher
|
38
|
-
# needs to be created on the next call
|
39
|
-
@last_cipher_key = cipher_key
|
40
|
-
# return the cipher
|
41
|
-
@cipher
|
14
|
+
String.create_simple_box key
|
42
15
|
end
|
43
16
|
end
|
@@ -2,10 +2,13 @@ module EasyEncryption
|
|
2
2
|
|
3
3
|
module ClassMethods
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
attr_reader :simple_box
|
6
|
+
|
7
|
+
# creates a simple_box using the passed key
|
8
|
+
#
|
9
|
+
# created_simple_box('secret')
|
10
|
+
def create_simple_box(key)
|
11
|
+
@simple_box ||= RbNaCl::SimpleBox.from_secret_key(RbNaCl::Hash.sha256(key))
|
9
12
|
end
|
10
13
|
end
|
11
14
|
end
|
@@ -2,35 +2,19 @@ module EasyEncryption
|
|
2
2
|
|
3
3
|
module InstanceMethods
|
4
4
|
|
5
|
-
# encrypts a string
|
6
|
-
def encrypt
|
7
|
-
|
8
|
-
cipher.encrypt self
|
5
|
+
# encrypts a string
|
6
|
+
def encrypt
|
7
|
+
simple_box.encrypt self
|
9
8
|
end
|
10
9
|
|
11
|
-
# decrypts a string
|
12
|
-
def decrypt
|
13
|
-
|
14
|
-
cipher.decrypt self
|
10
|
+
# decrypts a string
|
11
|
+
def decrypt
|
12
|
+
simple_box.decrypt self
|
15
13
|
end
|
16
14
|
|
17
|
-
#
|
18
|
-
def
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
# returns a Gibberish cipher
|
25
|
-
def cipher
|
26
|
-
|
27
|
-
if @cipher_key
|
28
|
-
# create/use a cipher in the context of a String instance
|
29
|
-
instance_exec @cipher_key, &EasyEncryption::CREATE_CIPHER
|
30
|
-
else
|
31
|
-
# use the singleton class's cipher
|
32
|
-
self.class.cipher
|
33
|
-
end
|
15
|
+
# references simple_box on the singleton class
|
16
|
+
def simple_box
|
17
|
+
self.class.simple_box
|
34
18
|
end
|
35
19
|
end
|
36
20
|
end
|
@@ -2,26 +2,14 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe EasyEncryption do
|
5
|
-
|
6
|
-
before(:all) do
|
7
|
-
EasyEncryption.setup 'default_password'
|
8
|
-
end
|
9
5
|
|
10
|
-
let(:
|
6
|
+
let(:key) { 'test' }
|
11
7
|
|
12
8
|
describe '.create_cipher' do
|
13
|
-
after { remove_instance_variable :@cipher }
|
14
|
-
|
15
|
-
it 'should create a cipher in the context of the caller' do
|
16
|
-
instance_exec default_password, &EasyEncryption::CREATE_CIPHER
|
17
|
-
@cipher.should be_an_instance_of(Gibberish::AES)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe '.cipher_key=' do
|
22
9
|
|
23
|
-
it 'should
|
24
|
-
|
10
|
+
it 'should create a simple box on String' do
|
11
|
+
EasyEncryption.setup key
|
12
|
+
String.simple_box.should be_an_instance_of(RbNaCl::SimpleBox)
|
25
13
|
end
|
26
14
|
end
|
27
15
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/string_spec.rb
CHANGED
@@ -4,70 +4,31 @@ require 'spec_helper'
|
|
4
4
|
describe String do
|
5
5
|
|
6
6
|
before(:all) do
|
7
|
-
EasyEncryption.setup '
|
7
|
+
EasyEncryption.setup 'test'
|
8
8
|
end
|
9
9
|
|
10
|
-
let(:default_password) { EasyEncryption.cipher_key }
|
11
|
-
let(:password) { 'password' }
|
12
10
|
let(:text) { 'foo' }
|
13
|
-
let(:
|
11
|
+
let(:encrypted_text) {
|
12
|
+
simple_box = RbNaCl::SimpleBox.from_secret_key(RbNaCl::Hash.sha256('test'))
|
13
|
+
simple_box.encrypt text
|
14
|
+
}
|
14
15
|
|
15
16
|
describe '#encrypt' do
|
16
17
|
|
17
|
-
context 'with
|
18
|
+
context 'with a string instance' do
|
18
19
|
|
19
|
-
it 'encrypts a string' do
|
20
|
-
|
21
|
-
decrypted = `echo "#{encrypted}" | openssl enc -d -aes-256-cbc -a -k "#{default_password}"`
|
22
|
-
decrypted.should eql(text)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with cipher key set on a String instance' do
|
27
|
-
|
28
|
-
it 'encrypts a string' do
|
29
|
-
text.cipher_key = password
|
30
|
-
encrypted = text.encrypt
|
31
|
-
decrypted = `echo "#{encrypted}" | openssl enc -d -aes-256-cbc -a -k "#{password}"`
|
32
|
-
decrypted.should eql(text)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'with cipher key passed to a String instance' do
|
37
|
-
|
38
|
-
it 'encrypts a string' do
|
39
|
-
encrypted = text.encrypt password
|
40
|
-
decrypted = `echo "#{encrypted}" | openssl enc -d -aes-256-cbc -a -k "#{password}"`
|
41
|
-
decrypted.should eql(text)
|
20
|
+
it 'encrypts a string instance' do
|
21
|
+
text.encrypt.encoding.should eql(Encoding::BINARY)
|
42
22
|
end
|
43
23
|
end
|
44
24
|
end
|
45
25
|
|
46
26
|
describe '#decrypt' do
|
47
27
|
|
48
|
-
context 'with
|
49
|
-
|
50
|
-
it 'decrypts a string' do
|
51
|
-
encrypted = `echo "foo" | openssl enc -aes-256-cbc -a -k "#{default_password}"`
|
52
|
-
decrypted = encrypted.decrypt.chomp
|
53
|
-
decrypted.should eql('foo')
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context 'with cipher key set on a String instance' do
|
58
|
-
|
59
|
-
it 'decrypts a string' do
|
60
|
-
encrypted = `echo "foo" | openssl enc -aes-256-cbc -a -k "#{password}"`
|
61
|
-
encrypted.cipher_key = password
|
62
|
-
encrypted.decrypt.should eql(console_text)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context 'with cipher key passed to a String instance' do
|
28
|
+
context 'with a string instance' do
|
67
29
|
|
68
|
-
it 'decrypts a string' do
|
69
|
-
|
70
|
-
encrypted.decrypt(password).should eql(console_text)
|
30
|
+
it 'decrypts a string instance' do
|
31
|
+
encrypted_text.decrypt.should eql(text)
|
71
32
|
end
|
72
33
|
end
|
73
34
|
end
|
metadata
CHANGED
@@ -1,85 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_encryption
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Ervans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 10.1.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 10.1.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 2.14.1
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 2.14.1
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rbnacl
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - '>='
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: gibberish
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - '>='
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
61
|
+
version: 3.0.0
|
76
62
|
type: :runtime
|
77
63
|
prerelease: false
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
79
65
|
requirements:
|
80
|
-
- -
|
66
|
+
- - "~>"
|
81
67
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
68
|
+
version: 3.0.0
|
83
69
|
description: Check the github page for a detailed description
|
84
70
|
email:
|
85
71
|
- aervans@gmail.com
|
@@ -87,8 +73,8 @@ executables: []
|
|
87
73
|
extensions: []
|
88
74
|
extra_rdoc_files: []
|
89
75
|
files:
|
90
|
-
- .gitignore
|
91
|
-
- .rspec
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
92
78
|
- Gemfile
|
93
79
|
- LICENSE.txt
|
94
80
|
- README.md
|
@@ -101,7 +87,7 @@ files:
|
|
101
87
|
- spec/easy_encryption_spec.rb
|
102
88
|
- spec/spec_helper.rb
|
103
89
|
- spec/string_spec.rb
|
104
|
-
homepage: https://github.com/
|
90
|
+
homepage: https://github.com/aervans/easyencryption
|
105
91
|
licenses:
|
106
92
|
- MIT
|
107
93
|
metadata: {}
|
@@ -111,20 +97,20 @@ require_paths:
|
|
111
97
|
- lib
|
112
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
113
99
|
requirements:
|
114
|
-
- -
|
100
|
+
- - ">="
|
115
101
|
- !ruby/object:Gem::Version
|
116
102
|
version: '0'
|
117
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
104
|
requirements:
|
119
|
-
- -
|
105
|
+
- - ">="
|
120
106
|
- !ruby/object:Gem::Version
|
121
107
|
version: '0'
|
122
108
|
requirements: []
|
123
109
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.0
|
110
|
+
rubygems_version: 2.2.0
|
125
111
|
signing_key:
|
126
112
|
specification_version: 4
|
127
|
-
summary: Adds easy to use encrypt and decrypt methods to
|
113
|
+
summary: Adds easy to use encrypt and decrypt methods to string instances.
|
128
114
|
test_files:
|
129
115
|
- spec/easy_encryption_spec.rb
|
130
116
|
- spec/spec_helper.rb
|