encryption 1.0.2 → 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/.rspec +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/{LICENSE → MIT-LICENSE} +0 -0
- data/README.md +99 -62
- data/encryption.gemspec +3 -3
- data/lib/configuration.rb +2 -0
- data/lib/configuration/base.rb +39 -0
- data/lib/configuration/symmetric.rb +16 -0
- data/lib/encryption.rb +12 -12
- data/lib/helpers.rb +0 -0
- data/lib/modules.rb +3 -0
- data/lib/modules/asymmetric.rb +4 -0
- data/lib/modules/asymmetric/keypair.rb +27 -0
- data/lib/modules/asymmetric/pkey.rb +30 -0
- data/lib/modules/asymmetric/private_key.rb +15 -0
- data/lib/modules/asymmetric/public_key.rb +15 -0
- data/lib/modules/encryptor.rb +6 -0
- data/lib/{encryption/encryptor.rb → modules/symmetric.rb} +16 -21
- data/spec/configuration/base_spec.rb +8 -0
- data/spec/configuration/symmetric_spec.rb +47 -0
- data/spec/encryption/asymmetric/integration_spec.rb +22 -0
- data/spec/encryption/asymmetric/keypair_spec.rb +18 -0
- data/spec/encryption/asymmetric/private_key_spec.rb +31 -0
- data/spec/encryption/asymmetric/public_key_spec.rb +31 -0
- data/spec/encryption/symmetric_global_spec.rb +48 -0
- data/spec/encryption/symmetric_instance_spec.rb +51 -0
- data/spec/spec_helper.rb +4 -2
- metadata +30 -15
- checksums.yaml +0 -15
- data/lib/encryption/configuration.rb +0 -41
- data/lib/encryption/string_helper.rb +0 -30
- data/spec/encryption_spec.rb +0 -55
- data/spec/modules/configuration_spec.rb +0 -52
- data/spec/modules/encryptor_spec.rb +0 -61
- data/spec/modules/string_helper_spec.rb +0 -38
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format progress
|
2
|
+
--format progress
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/{LICENSE → MIT-LICENSE}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -6,70 +6,107 @@ Encryption
|
|
6
6
|
|
7
7
|
A simple wrapper for the OpenSSL Cipher library
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
```ruby
|
15
|
-
Encryption.key = 'your encryption key'
|
16
|
-
enc_str = Encryption.encrypt "data to encrypt" # non-human readable string
|
17
|
-
Encryption.decrypt enc_str # "data to encrypt"
|
18
|
-
```
|
19
|
-
|
20
|
-
Installation
|
21
|
-
------------
|
22
|
-
```bash
|
23
|
-
gem install encryption
|
24
|
-
```
|
25
|
-
|
26
|
-
Configuration
|
27
|
-
-------------
|
28
|
-
You can pass the config settings with a configuration block
|
29
|
-
```ruby
|
30
|
-
Encryption.config do |config|
|
31
|
-
key: 'encryption key',
|
32
|
-
iv: 'initialization vector', # Optional
|
33
|
-
cipher: 'aes-256-cbc' # Optional. Defaults to `aes-256-cbc`
|
34
|
-
end
|
35
|
-
```
|
36
|
-
`Encryption::key`: is the encryption key. has to be set to use the encryption / decryption methods. Usually about 30+ charecters long.
|
37
|
-
`Encryption::iv`: _[Optional]_ the initialization vector. Defaults to the charecter "\0".
|
38
|
-
`Encryption::cipher`: _[Optional]_ the encryption algorithm to be used. Defaults to "aes-256-cbc".
|
39
|
-
|
40
|
-
Advanced
|
41
|
-
--------
|
42
|
-
In some cases you'll need a separate instance of the encryptor, rather than a global one.
|
43
|
-
To do that you just have to create a new instance of the `Encryption::Encryptor` class.
|
44
|
-
```ruby
|
45
|
-
encryptor = Encryption::Encryptor.new
|
46
|
-
encryptor.key = 'encryption key'
|
47
|
-
encryptor.iv = 'initialization vector' # Optional
|
48
|
-
encryptor.cipher = 'aes-128-cbc' # Optional
|
49
|
-
# Or you can configure it with a block
|
50
|
-
encryptor.config do |config|
|
51
|
-
key: 'your encryption key'
|
52
|
-
end
|
53
|
-
data = "data to encrypt"
|
54
|
-
enc_str = encryptor.encrypt(data) # Encrypt the string
|
55
|
-
dec_str = encryptor.decrypt(enc_str) # Decrypt it
|
56
|
-
data == dec_str # true
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
Run this command
|
12
|
+
```bash
|
13
|
+
gem install encryption
|
57
14
|
```
|
58
|
-
|
59
|
-
-------------
|
60
|
-
The gem adds a few helper methods to the String class. You can use them as follows:
|
15
|
+
or add this line to your `Gemfile`
|
61
16
|
```ruby
|
62
|
-
|
63
|
-
"this is a secret".encrypt!
|
64
|
-
"3nCRYpteD DaTA".decrypt
|
65
|
-
"3nCRYpteD DaTA".decrypt!
|
17
|
+
gem "encryption"
|
66
18
|
```
|
67
|
-
__Note:__ To use the string helpers you have t oset the encryption with `Encryption.key` or with a configuration block.
|
68
19
|
|
69
|
-
|
70
|
-
|
71
|
-
|
20
|
+
Symmetric encryption
|
21
|
+
--------------------
|
22
|
+
|
23
|
+
Using a global instance of the Encryption class
|
24
|
+
-----------------------------------------------
|
25
|
+
A simple example of how the gem works:
|
26
|
+
```ruby
|
27
|
+
Encryption.key = "Secretly yours,\n very long encryption key"
|
28
|
+
data = "this is to remain secret"
|
29
|
+
encrypted_str = Encryption.encrypt(data)
|
30
|
+
Encryption.decrypt(encrypted_str) == data # true
|
31
|
+
```
|
32
|
+
|
33
|
+
Using own instance of the Encryption class
|
34
|
+
------------------------------------------
|
35
|
+
Sometimes it is useful to use an own instance with custom settings, rather than the global Encryption instance. Here is how you can achieve it.
|
36
|
+
```ruby
|
37
|
+
encryptor = Encryption::Symmetric.new
|
38
|
+
encryptor.key = "Secretly yours,\n very long encryption key"
|
39
|
+
data = "this is to remain secret"
|
40
|
+
encrypted_str = encryptor.encrypt(data)
|
41
|
+
encryptor.decrypt(encrypted_str) == data # true
|
42
|
+
```
|
43
|
+
|
44
|
+
Configuration
|
45
|
+
-------------
|
46
|
+
For symmetric encryption / decryption you need to set an encryption key. The rest of the settings are optional. Here is a list of all of them:
|
47
|
+
`Encryption.key` - Your encryption key
|
48
|
+
`Encryption.iv # Optional` - Encryption initialization vector. Defaults to the charecter `"\0"`
|
49
|
+
`Encryption.cipher # Optional` - Your encryption algorithm. Defaults to `aes-256-cbc`
|
50
|
+
Run `openssl list-cipher-commands` in the terminal to list all installed ciphers or call `OpenSSL::Cipher.ciphers` in _Ruby_, which will return an array, containing all available algorithms.
|
51
|
+
|
52
|
+
You can optionally configure both a global instance and a custom instance with a __block__:
|
53
|
+
```ruby
|
54
|
+
Encryption.config do |config|
|
55
|
+
config.key = "don't look at me!"
|
56
|
+
config.iv = "is there a better way to initialize OpenSSL?"
|
57
|
+
config.cipher = "camellia-128-ecb" # if you feel adventurous
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
Asymmetric encryption (public/private key encryption)
|
62
|
+
-----------------------------------------------------
|
63
|
+
The `encryption` gem also provides easier synax for asymmetric encryption.
|
64
|
+
|
65
|
+
Generating keypair
|
66
|
+
------------------
|
67
|
+
```ruby
|
68
|
+
keypair = Encryption::Keypair.new # Accepts two optional arguments size = 2048 and password = nil
|
69
|
+
keypair.public_key # Instance of Encryption::PublicKey
|
70
|
+
keypair.private_key # Instance of Encryption::PrivateKey
|
71
|
+
# Or this for short
|
72
|
+
public_key, private_key = Encryption::Keypair.generate(2048)
|
73
|
+
|
74
|
+
# Then you can export each to string
|
75
|
+
private_key.to_s
|
76
|
+
|
77
|
+
# or to PEM format
|
78
|
+
private_key.to_pem
|
79
|
+
|
80
|
+
# and optionally encrypt is with a passphrase
|
81
|
+
private_key.to_pem('passphrase')
|
82
|
+
```
|
83
|
+
|
84
|
+
`Encryption::PublicKey` and `Encryption::PrivateKey`
|
85
|
+
----------------------------------------------------
|
86
|
+
Both classes have the same syntax
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
# Import an existing key
|
90
|
+
Encryption::PublicKey.new(filename[, password]) # From file
|
91
|
+
Encryption::PublicKey.new(string[, password]) # From string
|
92
|
+
|
93
|
+
# Encrypt / Decrypt data
|
94
|
+
public_key = Encryption::PublicKey.new("existing key")
|
95
|
+
public_key.encrypt("Hello!")
|
96
|
+
public_key.encrypt("H3LL0¡")
|
97
|
+
|
98
|
+
# Note that you can use both public and private keys to encrypt and decrypt data
|
99
|
+
```
|
100
|
+
|
101
|
+
<!---
|
102
|
+
Helpers
|
103
|
+
-------
|
104
|
+
String helpers
|
105
|
+
--------------
|
106
|
+
|
107
|
+
Array helpers
|
108
|
+
-------------
|
72
109
|
|
73
|
-
|
74
|
-
|
75
|
-
|
110
|
+
Hash helpers
|
111
|
+
------------
|
112
|
+
-->
|
data/encryption.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
|
1
|
+
require 'date'
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'encryption'
|
5
|
-
s.version = '1.0
|
5
|
+
s.version = '1.1.0'
|
6
6
|
s.date = Date.today.to_s
|
7
7
|
s.summary = 'A simple wrapper for the OpenSSL Cipher library'
|
8
|
-
s.description = 'Encryption provides a simple interface for encryption and decryption with the OpenSSL Cipher library'
|
8
|
+
s.description = 'Encryption provides a simple interface for symmetric / asymmetric encryption and decryption with the OpenSSL Cipher library'
|
9
9
|
s.authors = ['Itay Grudev']
|
10
10
|
s.email = ['itay.grudev@gmail.com']
|
11
11
|
s.homepage = 'https://github.com/Itehnological/encryption'
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Encryption
|
2
|
+
module Configuration
|
3
|
+
class Base
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@config = { }
|
7
|
+
end
|
8
|
+
|
9
|
+
def config
|
10
|
+
yield self
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(name, *args)
|
14
|
+
|
15
|
+
return @config[name.to_sym] if is_valid_getter(name)
|
16
|
+
return @config[name[0..-2].to_sym] = args[0] if is_valid_setter(name)
|
17
|
+
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def respond_to?(name)
|
22
|
+
return true if is_valid_getter(name) or is_valid_setter(name)
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def is_valid_getter(name)
|
29
|
+
@config.has_key? name.to_sym
|
30
|
+
end
|
31
|
+
|
32
|
+
def is_valid_setter(name)
|
33
|
+
name = name.to_s
|
34
|
+
name[-1, 1] == '=' and @config.has_key? name[0..-2].to_sym
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/encryption.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
String.send(:include, Encryption::String)
|
1
|
+
require_relative 'configuration.rb'
|
2
|
+
require_relative 'modules.rb'
|
3
|
+
require_relative 'helpers.rb'
|
6
4
|
|
7
5
|
module Encryption
|
8
6
|
|
9
|
-
@@
|
7
|
+
@@instance = nil # An instance to Encryption::Symmetric
|
10
8
|
|
11
9
|
def self.method_missing(name, *args, &block)
|
12
10
|
initalize_own_instance
|
13
11
|
|
14
|
-
if @@
|
15
|
-
return @@
|
12
|
+
if @@instance.respond_to?(name)
|
13
|
+
return @@instance.send(name, *args, &block)
|
16
14
|
end
|
17
15
|
|
18
16
|
super
|
@@ -20,14 +18,16 @@ module Encryption
|
|
20
18
|
|
21
19
|
def self.respond_to?(name)
|
22
20
|
initalize_own_instance
|
23
|
-
|
21
|
+
|
22
|
+
return true if @@instance.respond_to?(name)
|
24
23
|
super
|
25
24
|
end
|
26
25
|
|
27
|
-
private
|
26
|
+
private
|
27
|
+
|
28
28
|
def self.initalize_own_instance
|
29
|
-
if @@
|
30
|
-
@@
|
29
|
+
if @@instance.nil?
|
30
|
+
@@instance = Encryption::Symmetric.new
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
data/lib/helpers.rb
ADDED
File without changes
|
data/lib/modules.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Encryption
|
2
|
+
class Keypair
|
3
|
+
|
4
|
+
def initialize(size = 2048, password = nil)
|
5
|
+
@keypair = OpenSSL::PKey::RSA.new(size)
|
6
|
+
@password = password
|
7
|
+
end
|
8
|
+
|
9
|
+
def public_key
|
10
|
+
PublicKey.new(@keypair.public_key.to_s, @password)
|
11
|
+
end
|
12
|
+
|
13
|
+
def private_key
|
14
|
+
PrivateKey.new(@keypair.to_s, @password)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.generate(size = 2048, password = nil)
|
18
|
+
keypair = OpenSSL::PKey::RSA.new(size)
|
19
|
+
|
20
|
+
public_key = PublicKey.new(keypair.public_key.to_s, password)
|
21
|
+
private_key = PrivateKey.new(keypair.to_s, password)
|
22
|
+
|
23
|
+
return public_key, private_key
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Encryption
|
2
|
+
class PKey
|
3
|
+
|
4
|
+
def initialize(data, password = nil)
|
5
|
+
if File.exist?(data)
|
6
|
+
data = File.read(data)
|
7
|
+
end
|
8
|
+
|
9
|
+
if password.nil?
|
10
|
+
@key = OpenSSL::PKey::RSA.new(data)
|
11
|
+
else
|
12
|
+
@key = OpenSSL::PKey::RSA.new(data, password)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
@key.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_pem(password = nil)
|
21
|
+
if password.nil? or password.empty?
|
22
|
+
return @key.to_pem
|
23
|
+
end
|
24
|
+
|
25
|
+
cipher = OpenSSL::Cipher::Cipher.new('des3')
|
26
|
+
@key.to_pem(cipher, password)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -1,14 +1,10 @@
|
|
1
1
|
require 'openssl'
|
2
2
|
|
3
3
|
module Encryption
|
4
|
-
class
|
5
|
-
|
6
|
-
@cipher = nil
|
7
|
-
@decipher = nil
|
8
|
-
@configuration = nil
|
9
|
-
|
4
|
+
class Symmetric
|
5
|
+
|
10
6
|
def initialize
|
11
|
-
@configuration = Encryption::Configuration.new
|
7
|
+
@configuration = Encryption::Configuration::Symmetric.new
|
12
8
|
end
|
13
9
|
|
14
10
|
def method_missing(name, *args, &block)
|
@@ -24,37 +20,36 @@ module Encryption
|
|
24
20
|
end
|
25
21
|
|
26
22
|
def encrypt(data)
|
27
|
-
|
28
|
-
|
23
|
+
cipher_init
|
24
|
+
@cipher.update(data) + @cipher.final
|
29
25
|
end
|
30
26
|
|
31
27
|
def decrypt(data)
|
32
|
-
|
33
|
-
|
28
|
+
decipher_init
|
29
|
+
@decipher.update(data) + @decipher.final
|
34
30
|
end
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
what.iv = @configuration.iv
|
40
|
-
end
|
41
|
-
|
42
|
-
def _cipher
|
32
|
+
private
|
33
|
+
|
34
|
+
def cipher_init
|
43
35
|
if @cipher.nil?
|
44
36
|
@cipher = OpenSSL::Cipher.new(@configuration.cipher)
|
45
37
|
@cipher.encrypt
|
46
38
|
end
|
47
39
|
|
48
|
-
@cipher
|
40
|
+
@cipher.key = @configuration.key
|
41
|
+
@cipher.iv = @configuration.iv
|
49
42
|
end
|
50
43
|
|
51
|
-
def
|
44
|
+
def decipher_init
|
52
45
|
if @decipher.nil?
|
53
46
|
@decipher = OpenSSL::Cipher.new(@configuration.cipher)
|
54
47
|
@decipher.decrypt
|
55
48
|
end
|
56
49
|
|
57
|
-
@decipher
|
50
|
+
@decipher.key = @configuration.key
|
51
|
+
@decipher.iv = @configuration.iv
|
58
52
|
end
|
53
|
+
|
59
54
|
end
|
60
55
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Encryption::Configuration::Symmetric do
|
5
|
+
|
6
|
+
it 'should be configurable with a block' do
|
7
|
+
key = String.random
|
8
|
+
iv = String.random
|
9
|
+
cipher = String.random
|
10
|
+
|
11
|
+
@config = Encryption::Configuration::Symmetric.new
|
12
|
+
@config.config do |config|
|
13
|
+
config.cipher = cipher
|
14
|
+
config.key = key
|
15
|
+
config.iv = iv
|
16
|
+
end
|
17
|
+
|
18
|
+
@config.key.should == key
|
19
|
+
@config.iv.should == iv
|
20
|
+
@config.cipher.should == cipher
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'should set and return' do
|
24
|
+
before(:each) do
|
25
|
+
@config = Encryption::Configuration::Symmetric.new
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'key' do
|
29
|
+
value = String.random
|
30
|
+
@config.key = value
|
31
|
+
@config.key.should == value
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'iv' do
|
35
|
+
value = String.random
|
36
|
+
@config.iv = value
|
37
|
+
@config.iv.should == value
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'cipher' do
|
41
|
+
value = String.random
|
42
|
+
@config.cipher = value
|
43
|
+
@config.cipher.should == value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Encryption::Asymmetric' do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@string = String.random
|
7
|
+
keypair = Encryption::Keypair.new
|
8
|
+
@public_key = keypair.public_key
|
9
|
+
@private_key = keypair.private_key
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should encrypt with public key and then decrypt with private key' do
|
13
|
+
encrypted = @public_key.encrypt(@string)
|
14
|
+
@private_key.decrypt(encrypted).should == @string
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should encrypt with private key and then decrypt with public key' do
|
18
|
+
encrypted = @private_key.encrypt(@string)
|
19
|
+
@public_key.decrypt(encrypted).should == @string
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'openssl'
|
3
|
+
|
4
|
+
describe Encryption::Keypair do
|
5
|
+
|
6
|
+
it 'should generate keypairs with generate static method' do
|
7
|
+
public_key, private_key = Encryption::Keypair.generate
|
8
|
+
public_key.should be_an_instance_of(Encryption::PublicKey)
|
9
|
+
private_key.should be_an_instance_of(Encryption::PrivateKey)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should generate keypairs with keypair instance' do
|
13
|
+
keypair = Encryption::Keypair.new
|
14
|
+
keypair.public_key.should be_an_instance_of(Encryption::PublicKey)
|
15
|
+
keypair.private_key.should be_an_instance_of(Encryption::PrivateKey)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Encryption::PrivateKey do
|
4
|
+
|
5
|
+
describe do
|
6
|
+
before(:each) do
|
7
|
+
@key = Encryption::Keypair.new.private_key
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should generate encryption different than the original' do
|
11
|
+
string = String.random
|
12
|
+
@key.encrypt(string).should_not == string
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'should export to' do
|
17
|
+
before(:each) do
|
18
|
+
@keypair = Encryption::Keypair.new
|
19
|
+
@key = @keypair.private_key
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'string' do
|
23
|
+
@key.to_s.should be_an_instance_of(String)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'pem' do
|
27
|
+
@key.to_s.should be_an_instance_of(String)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Encryption::PublicKey do
|
4
|
+
|
5
|
+
describe do
|
6
|
+
before(:each) do
|
7
|
+
@key = Encryption::Keypair.new.public_key
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should generate encryption different than the original' do
|
11
|
+
string = String.random
|
12
|
+
@key.encrypt(string).should_not == string
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'should export to' do
|
17
|
+
before(:each) do
|
18
|
+
@keypair = Encryption::Keypair.new
|
19
|
+
@key = @keypair.public_key
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'string' do
|
23
|
+
@key.to_s.should be_an_instance_of(String)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'pem' do
|
27
|
+
@key.to_s.should be_an_instance_of(String)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'openssl'
|
3
|
+
|
4
|
+
describe Encryption do
|
5
|
+
|
6
|
+
it 'should be configurable with a block' do
|
7
|
+
key = String.random
|
8
|
+
iv = String.random
|
9
|
+
cipher = String.random
|
10
|
+
|
11
|
+
Encryption.config do |config|
|
12
|
+
config.key = key
|
13
|
+
config.iv = iv
|
14
|
+
config.cipher = cipher
|
15
|
+
end
|
16
|
+
|
17
|
+
Encryption.key.should == key
|
18
|
+
Encryption.iv.should == iv
|
19
|
+
Encryption.cipher.should == cipher
|
20
|
+
end
|
21
|
+
|
22
|
+
OpenSSL::Cipher.ciphers.each do |cipher|
|
23
|
+
next if ! cipher[-3, 3].nil? and ['gcm', 'fb1'].include? cipher[-3, 3].downcase
|
24
|
+
|
25
|
+
describe 'with cipher ' + cipher do
|
26
|
+
before(:each) do
|
27
|
+
Encryption.cipher = cipher
|
28
|
+
|
29
|
+
@string = String.random
|
30
|
+
Encryption.key = String.random
|
31
|
+
Encryption.iv = String.random
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should generate encryption different then the original string' do
|
35
|
+
encrypted = Encryption.encrypt(@string)
|
36
|
+
encrypted.should_not == @string
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should decrypt, encrypted values and match the original string' do
|
40
|
+
encrypted = Encryption.encrypt(@string)
|
41
|
+
decrypted = Encryption.decrypt(encrypted)
|
42
|
+
decrypted.should == @string
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'openssl'
|
3
|
+
|
4
|
+
describe Encryption::Symmetric do
|
5
|
+
|
6
|
+
it 'should be configurable with a block' do
|
7
|
+
key = String.random
|
8
|
+
iv = String.random
|
9
|
+
cipher = String.random
|
10
|
+
|
11
|
+
encryptor = Encryption::Symmetric.new
|
12
|
+
|
13
|
+
encryptor.config do |config|
|
14
|
+
config.key = key
|
15
|
+
config.iv = iv
|
16
|
+
config.cipher = cipher
|
17
|
+
end
|
18
|
+
|
19
|
+
encryptor.key.should == key
|
20
|
+
encryptor.iv.should == iv
|
21
|
+
encryptor.cipher.should == cipher
|
22
|
+
end
|
23
|
+
|
24
|
+
OpenSSL::Cipher.ciphers.each do |cipher|
|
25
|
+
next if ! cipher[-3, 3].nil? and ['gcm', 'fb1'].include? cipher[-3, 3].downcase
|
26
|
+
describe 'with cipher ' + cipher do
|
27
|
+
|
28
|
+
before(:each) do
|
29
|
+
@encryptor = Encryption::Symmetric.new
|
30
|
+
@encryptor.cipher = cipher
|
31
|
+
|
32
|
+
@string = String.random
|
33
|
+
@encryptor.key = String.random
|
34
|
+
@encryptor.iv = String.random
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should generate encryption different then the original string' do
|
38
|
+
encrypted = @encryptor.encrypt(@string)
|
39
|
+
encrypted.should_not == @string
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should decrypt, encrypted values and match the original string' do
|
43
|
+
encrypted = @encryptor.encrypt(@string)
|
44
|
+
decrypted = @encryptor.decrypt(encrypted)
|
45
|
+
decrypted.should == @string
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: encryption
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Itay Grudev
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-30 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
|
-
description: Encryption provides a simple interface for
|
14
|
-
with the OpenSSL Cipher library
|
14
|
+
description: Encryption provides a simple interface for symmetric / asymmetric encryption
|
15
|
+
and decryption with the OpenSSL Cipher library
|
15
16
|
email:
|
16
17
|
- itay.grudev@gmail.com
|
17
18
|
executables: []
|
@@ -22,41 +23,55 @@ files:
|
|
22
23
|
- .travis.yml
|
23
24
|
- Gemfile
|
24
25
|
- Gemfile.lock
|
25
|
-
- LICENSE
|
26
|
+
- MIT-LICENSE
|
26
27
|
- README.md
|
27
28
|
- Rakefile
|
28
29
|
- encryption.gemspec
|
30
|
+
- lib/configuration.rb
|
31
|
+
- lib/configuration/base.rb
|
32
|
+
- lib/configuration/symmetric.rb
|
29
33
|
- lib/encryption.rb
|
30
|
-
- lib/
|
31
|
-
- lib/
|
32
|
-
- lib/
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
36
|
-
-
|
34
|
+
- lib/helpers.rb
|
35
|
+
- lib/modules.rb
|
36
|
+
- lib/modules/asymmetric.rb
|
37
|
+
- lib/modules/asymmetric/keypair.rb
|
38
|
+
- lib/modules/asymmetric/pkey.rb
|
39
|
+
- lib/modules/asymmetric/private_key.rb
|
40
|
+
- lib/modules/asymmetric/public_key.rb
|
41
|
+
- lib/modules/encryptor.rb
|
42
|
+
- lib/modules/symmetric.rb
|
43
|
+
- spec/configuration/base_spec.rb
|
44
|
+
- spec/configuration/symmetric_spec.rb
|
45
|
+
- spec/encryption/asymmetric/integration_spec.rb
|
46
|
+
- spec/encryption/asymmetric/keypair_spec.rb
|
47
|
+
- spec/encryption/asymmetric/private_key_spec.rb
|
48
|
+
- spec/encryption/asymmetric/public_key_spec.rb
|
49
|
+
- spec/encryption/symmetric_global_spec.rb
|
50
|
+
- spec/encryption/symmetric_instance_spec.rb
|
37
51
|
- spec/spec_helper.rb
|
38
52
|
homepage: https://github.com/Itehnological/encryption
|
39
53
|
licenses:
|
40
54
|
- MIT
|
41
|
-
metadata: {}
|
42
55
|
post_install_message:
|
43
56
|
rdoc_options: []
|
44
57
|
require_paths:
|
45
58
|
- lib
|
46
59
|
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
47
61
|
requirements:
|
48
62
|
- - ! '>='
|
49
63
|
- !ruby/object:Gem::Version
|
50
64
|
version: '0'
|
51
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
52
67
|
requirements:
|
53
68
|
- - ! '>='
|
54
69
|
- !ruby/object:Gem::Version
|
55
70
|
version: '0'
|
56
71
|
requirements: []
|
57
72
|
rubyforge_project:
|
58
|
-
rubygems_version:
|
73
|
+
rubygems_version: 1.8.25
|
59
74
|
signing_key:
|
60
|
-
specification_version:
|
75
|
+
specification_version: 3
|
61
76
|
summary: A simple wrapper for the OpenSSL Cipher library
|
62
77
|
test_files: []
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
MGJmMmI0ZDNlMGYyY2RlZDFhNDVkYmZkNGU0YmRjZmQ1NTViMDI5Yw==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
NjhhNjdjMDIxMGM5ZjViOTFlMDQ4MDc4NWUyMWE3MjI0NWExMGVmZA==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
OGNiYzAyZDk1ZjY0NTkwNzZmMGI2ZWUzMjExMWU1ZmZjODQ3NzIzYTJjMmRm
|
10
|
-
MjJmNmNjNGY0ZGIyYTYxMTkxNzQ3NDk4MTAwN2U3MzBlZjY2NGM2M2IwMDFk
|
11
|
-
Y2E1MjNmMmM4ZGJkMzljNzZlMDk2NWY1ZGM4ZTU3Mzc2MDNhZTY=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZjU3MzZlNDkxNWZhMzUzZTQ4ZDQ1ZTEwMDAyMTFhZWYzOTI2Y2U1Y2Y1NzA3
|
14
|
-
ZWE5NmMxMjAwMGU0NTU3MmEzNWQyYzA0MTZlNGE3ZTljZGY5N2RhMjA0NjRj
|
15
|
-
MzI4ZGU2YTc5NzY5MzcxODdiNjg2OTE2NTRmY2ExNTI5MGEyY2E=
|
@@ -1,41 +0,0 @@
|
|
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..-2].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 = name.to_s
|
37
|
-
name[-1] == '=' and @config.has_key? name[0..-2].to_sym
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|
@@ -1,30 +0,0 @@
|
|
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
|
data/spec/encryption_spec.rb
DELETED
@@ -1,55 +0,0 @@
|
|
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
|
-
end
|
55
|
-
end
|
@@ -1,52 +0,0 @@
|
|
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
|
@@ -1,61 +0,0 @@
|
|
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
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
end
|
@@ -1,38 +0,0 @@
|
|
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
|