encryption 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ M2EyM2Q4MWUzM2VhZGE3ZmE5YmM2MGQ5N2Y2MTM0YzVjOWNhOTkwOQ==
5
+ data.tar.gz: !binary |-
6
+ YzQ1NjNkZDg0MWExNTZmN2YzNGMzNzczZTJjOGEzMWViNWYwOTc1ZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MjY3NDQzYzQwNTY3OTA5Yzc2MDRiYmE1N2Y5ODhjZWZlMmZhNjdiNDQ5ZmU3
10
+ ZDU4ZmEwNDJkZWY1ZmRkZjM2NTU5NTc2YzhmZDE4ODQ0ZTI2NzIyZTBmN2Rj
11
+ YjJlM2FkOGM1NmE3NmFiNjkyODQwMzI2NTc2NGY4NjhlNzhkNGQ=
12
+ data.tar.gz: !binary |-
13
+ ZTA3ZmZlM2I1OGZiNDg1OWQyZWQ2Nzc4ODA0NzliZmVmY2YyMzE0Yzc3NGJl
14
+ NzNjNWJmN2ZiMGZjMDZjZjU4NTU4NWRiOGM5OWI5ODAxZmY4MDQ1OTlkYWMw
15
+ NDE2YTdiOTM4YTE1MWIzNmI2ZmM0OWE5NWVhNGNiYjQ2MTgzYzU=
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ encryption (1.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.1)
10
+ rspec (2.13.0)
11
+ rspec-core (~> 2.13.0)
12
+ rspec-expectations (~> 2.13.0)
13
+ rspec-mocks (~> 2.13.0)
14
+ rspec-core (2.13.1)
15
+ rspec-expectations (2.13.0)
16
+ diff-lcs (>= 1.1.3, < 2.0)
17
+ rspec-mocks (2.13.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ encryption!
24
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Itay Grudev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ Encryption
2
+ ==========
3
+
4
+ [![Code Climate](https://codeclimate.com/github/Itehnological/encryption.png)](https://codeclimate.com/github/Itehnological/encryption)
5
+
6
+ A simple wrapper for the OpenSSL Cipher library
7
+
8
+ Usage
9
+ -----
10
+
11
+ Basic
12
+ -----
13
+ ```ruby
14
+ Encryption.key = 'your encryption key'
15
+ enc_str = Encryption.encrypt "data to encrypt" # non-human readable string
16
+ Encryption.decrypt enc_str # "data to encrypt"
17
+ ```
18
+
19
+ Installation
20
+ ------------
21
+ ```bash
22
+ gem install encryption
23
+ ```
24
+
25
+ Configuration
26
+ -------------
27
+ You can pass the config settings with a configuration block
28
+ ```ruby
29
+ Encryption.config do |config|
30
+ key: 'encryption key',
31
+ iv: 'initialization vector', # Optional
32
+ cipher: 'aes-256-cbc' # Optional. Defaults to `aes-256-cbc`
33
+ end
34
+ ```
35
+ `Encryption::key`: is the encryption key. has to be set to use the encryption / decryption methods. Usually about 30+ charecters long.
36
+ `Encryption::iv`: _[Optional]_ the initialization vector. Defaults to the charecter "\0".
37
+ `Encryption::cipher`: _[Optional]_ the encryption algorithm to be used. Defaults to "aes-256-cbc".
38
+
39
+ Advanced
40
+ --------
41
+ In some cases you'll need a separate instance of the encryptor, rather than a global one.
42
+ To do that you just have to create a new instance of the `Encryption::Encryptor` class.
43
+ ```ruby
44
+ encryptor = Encryption::Encryptor.new
45
+ encryptor.key = 'encryption key'
46
+ encryptor.iv = 'initialization vector' # Optional
47
+ encryptor.cipher = 'aes-128-cbc' # Optional
48
+ # Or you can configure it with a block
49
+ encryptor.config do |config|
50
+ key: 'your encryption key'
51
+ end
52
+ data = "data to encrypt"
53
+ enc_str = encryptor.encrypt(data) # Encrypt the string
54
+ dec_str = encryptor.decrypt(enc_str) # Decrypt it
55
+ data == dec_str # true
56
+ ```
57
+ String Helper
58
+ -------------
59
+ The gem adds a few helper methods to the String class. You can use them as follows:
60
+ ```ruby
61
+ "this is not a secret".encrypt # Non human readable string
62
+ "this is a secret".encrypt!
63
+ "3nCRYpteD DaTA".decrypt
64
+ "3nCRYpteD DaTA".decrypt!
65
+ ```
66
+ __Note:__ To use the string helpers you have t oset the encryption with `Encryption.key` or with a configuration block.
67
+
68
+ License
69
+ -------
70
+ This gem is published under The MIT License (MIT).
71
+
72
+ Author
73
+ ------
74
+ Itay Grudev - ![Itay Grudev](http://safemail.justlikeed.net/e/a5307c0c2dd405f756cab9f4c76cd63a.png)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ # Make RSpec the default task
6
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH << File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'encryption'
5
+ s.version = '1.0.1'
6
+ s.date = '2013-03-24'
7
+ s.summary = 'A simple wrapper for the OpenSSL Cipher library'
8
+ s.description = 'A simple wrapper for the OpenSSL Cipher library'
9
+ s.authors = ['Itay Grudev']
10
+ s.email = ['itay.grudev@gmail.com']
11
+ s.homepage = 'https://github.com/Itehnological/encryption'
12
+ s.files = `git ls-files`.split("\n")
13
+ s.require_paths = ['lib']
14
+ s.license = "MIT"
15
+ end
data/lib/encryption.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'encryption/configuration.rb'
2
+ require 'encryption/encryptor.rb'
3
+ require 'encryption/string_helper.rb'
4
+
5
+ String.send(:include, Encryption::String)
6
+
7
+ module Encryption
8
+
9
+ @@own_instance = nil
10
+
11
+ def self.method_missing(name, *args, &block)
12
+ initalize_own_instance
13
+
14
+ if @@own_instance.respond_to?(name)
15
+ return @@own_instance.send(name, *args, &block)
16
+ end
17
+
18
+ super
19
+ end
20
+
21
+ def self.respond_to?(name)
22
+ initalize_own_instance
23
+ return true if @@own_instance.respond_to?(name)
24
+ super
25
+ end
26
+
27
+ private
28
+ def self.initalize_own_instance
29
+ if @@own_instance.nil?
30
+ @@own_instance = Encryption::Encryptor.new
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,40 @@
1
+ module Encryption
2
+ class Configuration
3
+
4
+ def initialize
5
+ @config = {
6
+ key: nil,
7
+ iv: "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
8
+ cipher: 'aes-256-cbc'
9
+ }
10
+ end
11
+
12
+ def config
13
+ yield self
14
+ end
15
+
16
+ def method_missing(name, *args)
17
+
18
+ return @config[name.to_sym] if is_valid_getter(name)
19
+ return @config[name[0...-1].to_sym] = args[0] if is_valid_setter(name)
20
+
21
+ super
22
+ end
23
+
24
+ def respond_to?(name)
25
+ return true if is_valid_getter(name) or is_valid_setter(name)
26
+ super
27
+ end
28
+
29
+ private
30
+
31
+ def is_valid_getter(name)
32
+ @config.has_key? name.to_sym
33
+ end
34
+
35
+ def is_valid_setter(name)
36
+ name[-1] == '=' and @config.has_key? name[0...-1].to_sym
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,60 @@
1
+ require 'openssl'
2
+
3
+ module Encryption
4
+ class Encryptor
5
+
6
+ @cipher = nil
7
+ @decipher = nil
8
+ @configuration = nil
9
+
10
+ def initialize
11
+ @configuration = Encryption::Configuration.new
12
+ end
13
+
14
+ def method_missing(name, *args, &block)
15
+ if @configuration.respond_to? name
16
+ return @configuration.send(name, *args, &block)
17
+ end
18
+ super
19
+ end
20
+
21
+ def respond_to?(name)
22
+ return true if @configuration.respond_to? name
23
+ super
24
+ end
25
+
26
+ def encrypt(data)
27
+ prepare(_cipher)
28
+ _cipher.update(data) + _cipher.final
29
+ end
30
+
31
+ def decrypt(data)
32
+ prepare(_decipher)
33
+ _decipher.update(data) + _decipher.final
34
+ end
35
+
36
+ private
37
+ def prepare(what)
38
+ what.key = @configuration.key
39
+ what.iv = @configuration.iv
40
+ end
41
+
42
+ def _cipher
43
+ if @cipher.nil?
44
+ @cipher = OpenSSL::Cipher.new(@configuration.cipher)
45
+ @cipher.encrypt
46
+ end
47
+
48
+ @cipher
49
+ end
50
+
51
+ def _decipher
52
+ if @decipher.nil?
53
+ @decipher = OpenSSL::Cipher.new(@configuration.cipher)
54
+ @decipher.decrypt
55
+ end
56
+
57
+ @decipher
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,30 @@
1
+ module Encryption
2
+
3
+ #
4
+ # Extends String. Adds helper methods for encrypting / decrypting strings
5
+ #
6
+ module String
7
+
8
+ # Returns an encrypted version of a string
9
+ def encrypt
10
+ Encryption.encrypt self
11
+ end
12
+
13
+ # Replaces the string with an encrypted version of itself
14
+ def encrypt!
15
+ replace encrypt
16
+ end
17
+
18
+ # Returns a decrypted version of a string
19
+ def decrypt
20
+ Encryption.decrypt self
21
+ end
22
+
23
+ # Replaces the string with a decrypted version of itself
24
+ def decrypt!
25
+ replace decrypt
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encryption do
4
+
5
+ it { should respond_to :key }
6
+ it { should respond_to :iv }
7
+ it { should respond_to :cipher }
8
+ it { should respond_to :encrypt }
9
+ it { should respond_to :decrypt }
10
+
11
+ random = Proc.new { Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join) }
12
+
13
+ it "should be configurable with a block" do
14
+ key = random.call
15
+ iv = random.call
16
+ cipher = OpenSSL::Cipher.ciphers[rand(OpenSSL::Cipher.ciphers.count - 1)]
17
+
18
+ Encryption.config do |config|
19
+ config.cipher = cipher
20
+ config.key = key
21
+ config.iv = iv
22
+ end
23
+
24
+ Encryption.key.should eq key
25
+ Encryption.iv.should eq iv
26
+ Encryption.cipher.should eq cipher
27
+ end
28
+
29
+ describe "should encrypt / decrypt at ease" do
30
+
31
+ before(:each) do
32
+ Encryption.key = random.call
33
+ Encryption.iv = random.call
34
+ @original = random.call
35
+ end
36
+
37
+ it "should generate encrypted values different than the original" do
38
+ encrypted = Encryption.encrypt(@original)
39
+ encrypted.should_not eq @original
40
+ end
41
+
42
+ it "should decrypt encrypted values so they match the original string" do
43
+ encrypted = Encryption.encrypt(@original)
44
+ decrypted = Encryption.decrypt(encrypted)
45
+ decrypted.should eq @original
46
+ end
47
+
48
+ it "should generate same encrypted values for equal strings" do
49
+ encrypted1 = Encryption.encrypt(@original)
50
+ encrypted2 = Encryption.encrypt(@original)
51
+ encrypted1.should eq encrypted2
52
+ end
53
+
54
+ it "should have the same encoding for original and decrypted string" do
55
+ encrypted = Encryption.encrypt(@original)
56
+ decrypted = Encryption.decrypt(encrypted)
57
+ decrypted.encoding.should eq @original.encoding
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'openssl'
3
+
4
+ describe Encryption::Configuration do
5
+
6
+ it { should respond_to :key }
7
+ it { should respond_to :iv }
8
+ it { should respond_to :cipher }
9
+ it { should respond_to :config }
10
+
11
+ random = Proc.new { Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join) }
12
+
13
+ it "should be configurable with a block" do
14
+ key = random.call
15
+ iv = random.call
16
+ cipher = random.call
17
+
18
+ @config = Encryption::Configuration.new
19
+ @config.config do |config|
20
+ config.cipher = cipher
21
+ config.key = key
22
+ config.iv = iv
23
+ end
24
+
25
+ @config.key.should eq key
26
+ @config.iv.should eq iv
27
+ @config.cipher.should eq cipher
28
+ end
29
+
30
+ before(:each) do
31
+ @config = Encryption::Configuration.new
32
+ end
33
+
34
+ it "should set and return key" do
35
+ random_value = random.call
36
+ @config.key = random_value
37
+ @config.key.should == random_value
38
+ end
39
+
40
+ it "should set and return iv" do
41
+ random_value = random.call
42
+ @config.iv = random_value
43
+ @config.iv.should == random_value
44
+ end
45
+
46
+ it "should set and return ciper" do
47
+ random_value = random.call
48
+ @config.cipher = random_value
49
+ @config.cipher.should == random_value
50
+ end
51
+
52
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe Encryption::Encryptor do
4
+
5
+ it { should respond_to :key }
6
+ it { should respond_to :iv }
7
+ it { should respond_to :cipher }
8
+ it { should respond_to :encrypt }
9
+ it { should respond_to :decrypt }
10
+
11
+ random = Proc.new { Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join) }
12
+
13
+ it "should be configurable with a block" do
14
+ key = random.call
15
+ iv = random.call
16
+ cipher = random.call
17
+
18
+ @encryptor = Encryption::Encryptor.new
19
+ @encryptor.config do |config|
20
+ config.cipher = cipher
21
+ config.key = key
22
+ config.iv = iv
23
+ end
24
+
25
+ @encryptor.key.should eq key
26
+ @encryptor.iv.should eq iv
27
+ @encryptor.cipher.should eq cipher
28
+ end
29
+
30
+ describe "should encrypt / decrypt at ease" do
31
+
32
+ OpenSSL::Cipher.ciphers.each do |cipher|
33
+ before(:each) do
34
+ @encryptor = Encryption::Encryptor.new
35
+ @encryptor.cipher = cipher
36
+ @encryptor.key = random.call
37
+ @encryptor.iv = random.call
38
+ @original = random.call
39
+ end
40
+
41
+
42
+ it "should generate encrypted values different than the original" do
43
+ encrypted = @encryptor.encrypt(@original)
44
+ encrypted.should_not eq @original
45
+ end
46
+
47
+ it "should decrypt encrypted values so they match the original string" do
48
+ encrypted = @encryptor.encrypt(@original)
49
+ decrypted = @encryptor.decrypt(encrypted)
50
+ decrypted.should eq @original
51
+ end
52
+
53
+ it "should generate same encrypted values for equal strings" do
54
+ encrypted1 = @encryptor.encrypt(@original)
55
+ encrypted2 = @encryptor.encrypt(@original)
56
+ encrypted1.should eq encrypted2
57
+ end
58
+
59
+ it "should have the same encoding for original and decrypted string" do
60
+ encrypted = @encryptor.encrypt(@original)
61
+ decrypted = @encryptor.decrypt(encrypted)
62
+ decrypted.encoding.should eq @original.encoding
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe String do
4
+
5
+ it { should respond_to :encrypt }
6
+ it { should respond_to :encrypt! }
7
+ it { should respond_to :decrypt }
8
+ it { should respond_to :decrypt! }
9
+
10
+ before(:all) do
11
+ random = Proc.new { Digest::SHA256.hexdigest(([Time.now.to_s] * rand(3)).join) }
12
+
13
+ Encryption.config do |config|
14
+ config.key = random.call
15
+ config.iv = random.call
16
+ end
17
+
18
+ @random_string = random.call
19
+ end
20
+
21
+ it "should generate encrypted values different than the original" do
22
+ encrypted = @random_string.encrypt
23
+ encrypted.should_not eq @random_string
24
+ end
25
+
26
+ it "should decrypt encrypted values so they match the original string" do
27
+ encrypted = @random_string.encrypt
28
+ decrypted = encrypted.decrypt
29
+ decrypted.should eq @random_string
30
+ end
31
+
32
+ it "should generate same encrypted values for equal strings" do
33
+ encrypted1 = @random_string.encrypt
34
+ encrypted2 = @random_string.encrypt
35
+ encrypted1.should eq encrypted2
36
+ end
37
+
38
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'encryption'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encryption
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Itay Grudev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple wrapper for the OpenSSL Cipher library
14
+ email:
15
+ - itay.grudev@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .rspec
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - encryption.gemspec
27
+ - lib/encryption.rb
28
+ - lib/encryption/configuration.rb
29
+ - lib/encryption/encryptor.rb
30
+ - lib/encryption/string_helper.rb
31
+ - spec/encryption_spec.rb
32
+ - spec/modules/configuration_spec.rb
33
+ - spec/modules/encryptor_spec.rb
34
+ - spec/modules/string_helper_spec.rb
35
+ - spec/spec_helper.rb
36
+ homepage: https://github.com/Itehnological/encryption
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.0.3
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A simple wrapper for the OpenSSL Cipher library
60
+ test_files: []