coder_decorator 1.0.1 → 1.0.2

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: 632b576de3c05ef79d82ba749cba8f22f6f62fed
4
- data.tar.gz: 226f7e428d62b83dd1996f25a0c6d8c3364f4e44
3
+ metadata.gz: e4a202ad3cb054138882ff8ba09cc79bf4a58860
4
+ data.tar.gz: 42b3b4bcdae63fb410391f243d594bbe67ec34eb
5
5
  SHA512:
6
- metadata.gz: 7ed5494544fab2529cdefd5247ecefe87efe4e636b5d0a5d3d29121220db2974cf5b1698b985a2f0c84316aa31706f3a5b72f577832ea9bc0e233da8726325ae
7
- data.tar.gz: 25a6c1b6b5dd5ccf9e7352ee9972a2efe76fe0085cb8f478940bbdf7b4849921a301a96f3fe552c48f1e8c7e31c6b3146425eb589054ce72e5f714b58d0840bc
6
+ metadata.gz: 5b5b0524aa151aa4724526e65707672b38cc17bf78a309f09d4fd88c2563e4f665a1dd096f401de180cbad8ad6b9df12c03e9e0a13a5c2758d8055c07a210fed
7
+ data.tar.gz: 7e1f11176529551995a0676ef5993139d6b12267213579cc1d374a73e5484b374331489d31b41064d297d7ae7e0858b11760484474b0a76f7886e77b85452672
@@ -10,9 +10,10 @@ module CoderDecorator
10
10
  # "#{encrypted_data}--#{initial_vector}"
11
11
  #
12
12
  class Cipher < Coder
13
- def initialize(coder = nil, secret:, cipher: 'AES-256-CBC')
13
+ def initialize(coder = nil, secret:, old_secret: nil, cipher: 'AES-256-CBC')
14
14
  super(coder)
15
15
  @secret = secret
16
+ @old_secret = old_secret
16
17
  @cipher = ::OpenSSL::Cipher.new(cipher)
17
18
  @base64 = Coders::Base64.new
18
19
  end
@@ -27,15 +28,24 @@ module CoderDecorator
27
28
  end
28
29
 
29
30
  def decode(str)
30
- encrypted_data, iv = @base64.decode(str).split('--').map! { |v| @base64.decode(v) }
31
+ [@secret, @old_secret].each do |secret|
32
+ begin
33
+ return decrypt(secret, str)
34
+ rescue ::OpenSSL::Cipher::CipherError, ::TypeError
35
+ next
36
+ end
37
+ end
38
+ raise InvalidEncoding
39
+ end
40
+
41
+ private
42
+
43
+ def decrypt(secret, data)
44
+ encrypted_data, iv = @base64.decode(data).split('--').map! { |v| @base64.decode(v) }
31
45
  @cipher.decrypt
32
- @cipher.key = @secret
46
+ @cipher.key = secret
33
47
  @cipher.iv = iv
34
- begin
35
- coder.decode(@cipher.update(encrypted_data) << @cipher.final)
36
- rescue ::OpenSSL::Cipher::CipherError
37
- raise InvalidEncoding
38
- end
48
+ coder.decode(@cipher.update(encrypted_data) << @cipher.final)
39
49
  end
40
50
  end
41
51
  end
@@ -12,28 +12,31 @@ module CoderDecorator
12
12
  class HMAC < Coder
13
13
  REGEXP = /\A(.*)--(.*)\z/
14
14
 
15
- def initialize(coder = nil, secret:, digest: 'SHA1')
15
+ def initialize(coder = nil, secret:, old_secret: nil, digest: 'SHA1')
16
16
  super(coder)
17
17
  @secret = secret
18
+ @old_secret = old_secret
18
19
  @digest = ::OpenSSL::Digest.new(digest)
19
20
  end
20
21
 
21
22
  def encode(str)
22
23
  data = coder.encode(str)
23
- hmac = generate_hmac(data)
24
+ hmac = generate_hmac(@secret, data)
24
25
  "#{data}--#{hmac}"
25
26
  end
26
27
 
27
28
  def decode(str)
28
- data, hmac = REGEXP.match(str)&.captures
29
- raise InvalidEncoding unless data && hmac && secure_compare(hmac, generate_hmac(data))
29
+ match_data = REGEXP.match(str)
30
+ data, hmac = match_data && match_data.captures
31
+ secrets = [@secret, @old_secret]
32
+ raise InvalidEncoding unless data && hmac && secrets.any? { |secret| secure_compare(hmac, generate_hmac(secret, data)) }
30
33
  coder.decode(data)
31
34
  end
32
35
 
33
36
  private
34
37
 
35
- def generate_hmac(str)
36
- ::OpenSSL::HMAC.hexdigest(@digest.new, @secret, str)
38
+ def generate_hmac(secret, str)
39
+ ::OpenSSL::HMAC.hexdigest(@digest.new, secret, str)
37
40
  end
38
41
 
39
42
  def secure_compare(a, b)
metadata CHANGED
@@ -1,52 +1,52 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coder_decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jian Weihang
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ name: minitest
14
15
  requirement: !ruby/object:Gem::Requirement
15
16
  requirements:
16
17
  - - ">="
17
18
  - !ruby/object:Gem::Version
18
19
  version: '0'
19
- name: minitest
20
- prerelease: false
21
20
  type: :development
21
+ prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
+ name: rake
28
29
  requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - ">="
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
- name: rake
34
- prerelease: false
35
34
  type: :development
35
+ prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
+ name: rubocop
42
43
  requirement: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - ">="
45
46
  - !ruby/object:Gem::Version
46
47
  version: '0'
47
- name: rubocop
48
- prerelease: false
49
48
  type: :development
49
+ prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
@@ -69,11 +69,11 @@ files:
69
69
  - lib/coder_decorator/coders/rescue.rb
70
70
  - lib/coder_decorator/coders/zip.rb
71
71
  - lib/coder_decorator/errors.rb
72
- homepage:
72
+ homepage:
73
73
  licenses:
74
74
  - MIT
75
75
  metadata: {}
76
- post_install_message:
76
+ post_install_message:
77
77
  rdoc_options: []
78
78
  require_paths:
79
79
  - lib
@@ -81,16 +81,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ">="
83
83
  - !ruby/object:Gem::Version
84
- version: '0'
84
+ version: 2.2.0
85
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubyforge_project:
91
+ rubyforge_project:
92
92
  rubygems_version: 2.6.8
93
- signing_key:
93
+ signing_key:
94
94
  specification_version: 4
95
95
  summary: An encoding/decoding library with decorator pattern.
96
96
  test_files: []