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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df92c45b60b8051c992490f2289b2511a5643226
4
- data.tar.gz: a6dcb4e514c4ab76ac754416b43c01f3ea254582
3
+ metadata.gz: 80d50c158df4722786b31e9f644699453e4f8dfb
4
+ data.tar.gz: 12e6dac5ec2e15b3a8970235fc806d1e996c9a5a
5
5
  SHA512:
6
- metadata.gz: 2df8d1021815040d88521e5549d2e1cc87f3429f31e751784816d23ada0dcb96f231b734b48cfe92fee4ad2ef534bc4b5924a0bfaaca8b395283a1a0a2e32488
7
- data.tar.gz: 196722bab46f11745db385cf8c6977a7a9bbbbb65e099c6cbddf46e8fb01d8eba36dab6d5edb556fe906c47d0e89cdd04e3a42f409f902f491bfa3aee951c253
6
+ metadata.gz: 6c6cf7a7cc8bb1e1373fe45d6a0473c16b31492d319363210b7206585feead5ec9db5cb163401e4ef6cc203a525e03e0b92be5e921beceaf2190f53166162237
7
+ data.tar.gz: b0df1372c7a6682191f1ad4fea0c779720b86b571228fd07d97af3c626b7b3cc61139ef64c3a980baf4ab17f575af687b291f52b57f79241c2f834ac5a2ffdb8
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .DS_Store
2
2
  pkg
3
3
  tmp
4
+ Gemfile.lock
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>)
@@ -6,21 +6,46 @@ require 'parsel/json'
6
6
  require 'parsel/version'
7
7
 
8
8
  module Parsel
9
- def self.encrypt(key, data)
10
- encode cipher(:encrypt, key, data)
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
- def self.decrypt(key, data)
14
- cipher(:decrypt, key, decode(data))
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('AES-256-CBC').public_send(mode)
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 = 'f89209ffcdd1a225'
48
+ cipher.iv = iv
24
49
  cipher.update(data) + cipher.final
25
50
  end
26
51
 
@@ -1,11 +1,13 @@
1
1
  module Parsel
2
2
  module JSON
3
- def self.encrypt(key, data)
4
- Parsel.encrypt(key, ::JSON.dump(data))
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(key, data)
8
- ::JSON.load Parsel.decrypt(key, data)
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
@@ -1,11 +1,13 @@
1
1
  module Parsel
2
2
  module Marshal
3
- def self.encrypt(key, data)
4
- Parsel.encrypt(key, ::Marshal.dump(data))
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(key, data)
8
- ::Marshal.load Parsel.decrypt(key, data)
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
@@ -1,7 +1,7 @@
1
1
  module Parsel
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 2
4
+ MINOR = 3
5
5
  PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
@@ -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
@@ -1,3 +1,8 @@
1
1
  require 'bundler/setup'
2
2
  require 'parsel'
3
3
 
4
+ RSpec.configure do |config|
5
+ config.before do
6
+ Parsel.default_iv = Parsel::DEFAULT_IV
7
+ end
8
+ end
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.2.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: 2014-06-22 00:00:00.000000000 Z
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.2.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:
@@ -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