parsel 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +17 -0
- data/lib/parsel.rb +32 -7
- data/lib/parsel/json.rb +6 -4
- data/lib/parsel/marshal.rb +6 -4
- data/lib/parsel/version.rb +1 -1
- data/spec/parsel_spec.rb +22 -0
- data/spec/spec_helper.rb +5 -0
- metadata +3 -5
- data/Gemfile.lock +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80d50c158df4722786b31e9f644699453e4f8dfb
|
4
|
+
data.tar.gz: 12e6dac5ec2e15b3a8970235fc806d1e996c9a5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c6cf7a7cc8bb1e1373fe45d6a0473c16b31492d319363210b7206585feead5ec9db5cb163401e4ef6cc203a525e03e0b92be5e921beceaf2190f53166162237
|
7
|
+
data.tar.gz: b0df1372c7a6682191f1ad4fea0c779720b86b571228fd07d97af3c626b7b3cc61139ef64c3a980baf4ab17f575af687b291f52b57f79241c2f834ac5a2ffdb8
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -49,6 +49,23 @@ decrypted = Parsel::JSON.decrypt(secret_key, encrypted)
|
|
49
49
|
#=> {"user_id" => 1234}
|
50
50
|
```
|
51
51
|
|
52
|
+
### Custom Cipher IV
|
53
|
+
|
54
|
+
By default, this library uses a built-in IV. You may want to change that.
|
55
|
+
|
56
|
+
You can globally change the IV like the following:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
Parsel.default_iv = SecureRandom.hex(100)
|
60
|
+
```
|
61
|
+
|
62
|
+
You can also pass the IV to encrypt/decrypt methods.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Parsel.encrypt(secret_key, iv, data)
|
66
|
+
Parsel.decrypt(secret_key, iv, data)
|
67
|
+
```
|
68
|
+
|
52
69
|
## Maintainer
|
53
70
|
|
54
71
|
- Nando Vieira (<http://nandovieira.com.br>)
|
data/lib/parsel.rb
CHANGED
@@ -6,21 +6,46 @@ require 'parsel/json'
|
|
6
6
|
require 'parsel/version'
|
7
7
|
|
8
8
|
module Parsel
|
9
|
-
|
10
|
-
|
9
|
+
DEFAULT_IV = 'f89209ffcdd1a225'.freeze
|
10
|
+
CIPHER = 'AES-256-CBC'.freeze
|
11
|
+
|
12
|
+
def self.default_iv=(iv)
|
13
|
+
@default_iv = iv
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.default_iv
|
17
|
+
@default_iv
|
11
18
|
end
|
12
19
|
|
13
|
-
|
14
|
-
|
20
|
+
self.default_iv = DEFAULT_IV
|
21
|
+
|
22
|
+
def self.encrypt(*args)
|
23
|
+
encode cipher(:encrypt, *expand_args(args))
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.decrypt(*args)
|
27
|
+
key, iv, data = expand_args(args)
|
28
|
+
cipher(:decrypt, key, iv, decode(data))
|
15
29
|
rescue Exception
|
16
30
|
false
|
17
31
|
end
|
18
32
|
|
33
|
+
def self.expand_args(args)
|
34
|
+
if args.size == 2
|
35
|
+
iv = default_iv
|
36
|
+
key, data = args
|
37
|
+
else
|
38
|
+
key, iv, data = args
|
39
|
+
end
|
40
|
+
|
41
|
+
[key, iv, data]
|
42
|
+
end
|
43
|
+
|
19
44
|
private
|
20
|
-
def self.cipher(mode, key, data)
|
21
|
-
cipher = OpenSSL::Cipher.new(
|
45
|
+
def self.cipher(mode, key, iv, data)
|
46
|
+
cipher = OpenSSL::Cipher.new(CIPHER).public_send(mode)
|
22
47
|
cipher.key = Digest::SHA256.digest(key)
|
23
|
-
cipher.iv =
|
48
|
+
cipher.iv = iv
|
24
49
|
cipher.update(data) + cipher.final
|
25
50
|
end
|
26
51
|
|
data/lib/parsel/json.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module Parsel
|
2
2
|
module JSON
|
3
|
-
def self.encrypt(
|
4
|
-
|
3
|
+
def self.encrypt(*args)
|
4
|
+
key, iv, data = Parsel.expand_args(args)
|
5
|
+
Parsel.encrypt(key, iv, ::JSON.dump(data))
|
5
6
|
end
|
6
7
|
|
7
|
-
def self.decrypt(
|
8
|
-
|
8
|
+
def self.decrypt(*args)
|
9
|
+
key, iv, data = Parsel.expand_args(args)
|
10
|
+
::JSON.load Parsel.decrypt(key, iv, data)
|
9
11
|
rescue Exception
|
10
12
|
false
|
11
13
|
end
|
data/lib/parsel/marshal.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module Parsel
|
2
2
|
module Marshal
|
3
|
-
def self.encrypt(
|
4
|
-
|
3
|
+
def self.encrypt(*args)
|
4
|
+
key, iv, data = Parsel.expand_args(args)
|
5
|
+
Parsel.encrypt(key, iv, ::Marshal.dump(data))
|
5
6
|
end
|
6
7
|
|
7
|
-
def self.decrypt(
|
8
|
-
|
8
|
+
def self.decrypt(*args)
|
9
|
+
key, iv, data = Parsel.expand_args(args)
|
10
|
+
::Marshal.load Parsel.decrypt(key, iv, data)
|
9
11
|
rescue Exception
|
10
12
|
false
|
11
13
|
end
|
data/lib/parsel/version.rb
CHANGED
data/spec/parsel_spec.rb
CHANGED
@@ -15,4 +15,26 @@ describe Parsel do
|
|
15
15
|
it 'returns false when decryption fails' do
|
16
16
|
expect(Parsel.decrypt('abc', '123')).to be_falsy
|
17
17
|
end
|
18
|
+
|
19
|
+
it 'uses custom iv' do
|
20
|
+
iv = SecureRandom.hex(100)
|
21
|
+
encrypted = Parsel.encrypt(key, iv, 'hello')
|
22
|
+
|
23
|
+
expect(Parsel.decrypt(key, iv, encrypted)).to eq('hello')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'return false when decryption for custom iv fails' do
|
27
|
+
iv = SecureRandom.hex(100)
|
28
|
+
encrypted = Parsel.encrypt(key, iv, 'hello')
|
29
|
+
|
30
|
+
expect(Parsel.decrypt(key, encrypted)).to be_falsy
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'changes global iv' do
|
34
|
+
iv = SecureRandom.hex(100)
|
35
|
+
Parsel.default_iv = iv
|
36
|
+
encrypted = Parsel.encrypt(key, 'hello')
|
37
|
+
|
38
|
+
expect(Parsel.decrypt(key, iv, encrypted)).to eq('hello')
|
39
|
+
end
|
18
40
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -48,7 +48,6 @@ files:
|
|
48
48
|
- ".gitignore"
|
49
49
|
- ".rspec"
|
50
50
|
- Gemfile
|
51
|
-
- Gemfile.lock
|
52
51
|
- README.md
|
53
52
|
- Rakefile
|
54
53
|
- lib/parsel.rb
|
@@ -79,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
78
|
version: '0'
|
80
79
|
requirements: []
|
81
80
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.
|
81
|
+
rubygems_version: 2.4.5.1
|
83
82
|
signing_key:
|
84
83
|
specification_version: 4
|
85
84
|
summary: Encrypt and decrypt data with a given key.
|
@@ -88,4 +87,3 @@ test_files:
|
|
88
87
|
- spec/parsel/marshal_spec.rb
|
89
88
|
- spec/parsel_spec.rb
|
90
89
|
- spec/spec_helper.rb
|
91
|
-
has_rdoc:
|
data/Gemfile.lock
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
parsel (0.2.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
diff-lcs (1.2.5)
|
10
|
-
rake (10.3.2)
|
11
|
-
rspec (3.0.0)
|
12
|
-
rspec-core (~> 3.0.0)
|
13
|
-
rspec-expectations (~> 3.0.0)
|
14
|
-
rspec-mocks (~> 3.0.0)
|
15
|
-
rspec-core (3.0.2)
|
16
|
-
rspec-support (~> 3.0.0)
|
17
|
-
rspec-expectations (3.0.2)
|
18
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
-
rspec-support (~> 3.0.0)
|
20
|
-
rspec-mocks (3.0.2)
|
21
|
-
rspec-support (~> 3.0.0)
|
22
|
-
rspec-support (3.0.2)
|
23
|
-
|
24
|
-
PLATFORMS
|
25
|
-
ruby
|
26
|
-
|
27
|
-
DEPENDENCIES
|
28
|
-
parsel!
|
29
|
-
rake
|
30
|
-
rspec
|