easy_encryption 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7462e6d0b95b2deceeae6234766c5d179a77e6a1
4
+ data.tar.gz: 206ab0d885387883b14d533d40f6aae503a493ee
5
+ SHA512:
6
+ metadata.gz: 1899ad93eb959af929f8360ab17a2d2d41ae3ef8dddf6193a710570441f44444f8d85b129281cc47be9f93b4e868ae292a1e8d4a3c30c733c06e724776e3f671
7
+ data.tar.gz: 31d4560e6f980befd5d49fe9b096b95564706726a5794b3801a95b9dbabe1d2fc619b4ddb3d4b2b3a349ab981bc56b7eba78497570fb19f1d3ca92f73278b237
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ *.un~
20
+ .DS_STORE
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_encryption.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Adam Ervans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # EasyEncryption
2
+
3
+ Easy Encryption provides a simple means for encrypting and decrypting strings. Uses the Gibberish gem (https://github.com/mdp/gibberish) under the hood to do the actual encryption work.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'easy_encryption'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install easy_encryption
18
+
19
+ Finally, call setup on EasyEncryption passing your cipher key:
20
+ ```ruby
21
+ EasyEncryption.setup 'secret'
22
+ ```
23
+
24
+ In a Rails app you will need to set the default cipher key inside of your app's ::Application.configure block:
25
+ ```ruby
26
+ config.after_initialize do
27
+ EasyEncryption.setup 'secret'
28
+ end
29
+ ```
30
+
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
+ ## Usage
34
+
35
+ EasyEncryption provides two additional methods that can be called on any string.
36
+
37
+ `encrypt` will encrypt a string:
38
+ ```ruby
39
+ encrypted_data = 'foo'.encrypt
40
+ ```
41
+
42
+ `decrypt` will decrypt an encrypted string:
43
+ ```ruby
44
+ decrypted_data = encrypted_data.decrypt
45
+ ```
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
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_encryption/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_encryption"
8
+ spec.version = EasyEncryption::VERSION
9
+ spec.authors = ["Adam Ervans"]
10
+ spec.email = ["aervans@gmail.com"]
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 strings.}
13
+ spec.homepage = "https://github.com/arthrex/easy-encryption"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_dependency "gibberish"
26
+ end
@@ -0,0 +1,43 @@
1
+ require "easy_encryption/version"
2
+ require "easy_encryption/instance_methods"
3
+ require "easy_encryption/class_methods"
4
+ require "gibberish"
5
+
6
+ module EasyEncryption
7
+
8
+ # sets up EasyEncryption
9
+ def self.setup(cipher_key)
10
+ @cipher_key = cipher_key
11
+ String.send :include, InstanceMethods
12
+ String.send :extend, ClassMethods
13
+ true
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
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ module EasyEncryption
2
+
3
+ module ClassMethods
4
+
5
+ # returns a Gibberish cipher
6
+ def cipher
7
+ # create/use a cipher in the context of the String singleton
8
+ instance_exec EasyEncryption.cipher_key, &EasyEncryption::CREATE_CIPHER
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module EasyEncryption
2
+
3
+ module InstanceMethods
4
+
5
+ # encrypts a string. ex: "string".encrypt
6
+ def encrypt(cipher_key=nil)
7
+ @cipher_key = cipher_key if cipher_key
8
+ cipher.encrypt self
9
+ end
10
+
11
+ # decrypts a string. ex: "string".decrypt
12
+ def decrypt(cipher_key=nil)
13
+ @cipher_key = cipher_key if cipher_key
14
+ cipher.decrypt self
15
+ end
16
+
17
+ # setter to change the cipher key on an instance of String
18
+ def cipher_key=(cipher_key)
19
+ @cipher_key = cipher_key
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
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module EasyEncryption
2
+
3
+ module StringEncryption
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module EasyEncryption
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe EasyEncryption do
5
+
6
+ before(:all) do
7
+ EasyEncryption.setup 'default_password'
8
+ end
9
+
10
+ let(:default_password) { EasyEncryption.cipher_key }
11
+
12
+ 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
+
23
+ it 'should set the cipher key' do
24
+ expect(EasyEncryption.cipher_key=('password')).to eql('password')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,2 @@
1
+ require 'easy_encryption'
2
+ require 'pry'
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe String do
5
+
6
+ before(:all) do
7
+ EasyEncryption.setup 'default_password'
8
+ end
9
+
10
+ let(:default_password) { EasyEncryption.cipher_key }
11
+ let(:password) { 'password' }
12
+ let(:text) { 'foo' }
13
+ let(:console_text) { `echo "foo"` }
14
+
15
+ describe '#encrypt' do
16
+
17
+ context 'with cipher key set on String' do
18
+
19
+ it 'encrypts a string' do
20
+ encrypted = text.encrypt
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)
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '#decrypt' do
47
+
48
+ context 'with cipher key set on String' do
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
67
+
68
+ it 'decrypts a string' do
69
+ encrypted = `echo "foo" | openssl enc -aes-256-cbc -a -k "#{password}"`
70
+ encrypted.decrypt(password).should eql(console_text)
71
+ end
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_encryption
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Ervans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
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'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Check the github page for a detailed description
84
+ email:
85
+ - aervans@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - easy_encryption.gemspec
97
+ - lib/easy_encryption.rb
98
+ - lib/easy_encryption/class_methods.rb
99
+ - lib/easy_encryption/instance_methods.rb
100
+ - lib/easy_encryption/string_encryptor.rb
101
+ - lib/easy_encryption/version.rb
102
+ - spec/easy_encryption_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/string_spec.rb
105
+ homepage: https://github.com/arthrex/easy-encryption
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.0.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Adds easy to use encrypt and decrypt methods to strings.
129
+ test_files:
130
+ - spec/easy_encryption_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/string_spec.rb