lite-encryption 1.0.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e6341e27bca450a794f52c1e212acceb3c0ba906283183090d9b74cba762727
4
- data.tar.gz: 354c78a0d9ee5ad39ce6ed00fae615ee57978b3368f7403b58f551b8e51488f3
3
+ metadata.gz: d5f2a37b612cb9d2f2fe5a941af941b9d0423b9ce1232887830d08a5f4fa4bc0
4
+ data.tar.gz: e69ea20e36b30f0adcc60f753a117f3f8bc6825a1e9e2109fef1528bc993c951
5
5
  SHA512:
6
- metadata.gz: b2147b276a2324dcb858ba53809f2ba8be89d2bdac581bdef7d67a852ed5c1dd8821d3131367d799bfa5411bd296f99f48d9120d771cbf3b374af429ab419b6c
7
- data.tar.gz: 1d0829e71711850aaf6767c865c47ad136a017086493ff388703b5cb4c4200eb7524c812bd5ac1679e9aa953112fc4b69b65a6666ab17baee4efaf2e9f6a45ce
6
+ metadata.gz: 4b81e82c8885e661eb17f38d584874e7a59297e6f0f55f9f32cfbf73678516d16f7aaec3fa8d0f81534da87c2b62e55a799396c402b4bea40313640b719c0374
7
+ data.tar.gz: 6d1b39740a256ae0df3446895b818f04f7d48c83981f8af1be7236911694faaf2cc618a11a88de4ba5d0f404e27f13ac1eed78518612755ee35ac341edb63709
data/.rubocop.yml CHANGED
@@ -1,12 +1,16 @@
1
1
  require:
2
2
  - rubocop-performance
3
+ - rubocop-rake
3
4
  - rubocop-rspec
4
5
  AllCops:
5
- TargetRubyVersion: 2.6
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
6
8
  DisplayCopNames: true
7
9
  DisplayStyleGuide: true
8
- LineLength:
9
- Max: 100
10
+ Gemspec/RequiredRubyVersion:
11
+ Enabled: false
12
+ Layout/EmptyLinesAroundAttributeAccessor:
13
+ Enabled: true
10
14
  Layout/EmptyLinesAroundBlockBody:
11
15
  Exclude:
12
16
  - 'spec/**/**/*'
@@ -14,6 +18,14 @@ Layout/EmptyLinesAroundClassBody:
14
18
  EnforcedStyle: empty_lines_except_namespace
15
19
  Layout/EmptyLinesAroundModuleBody:
16
20
  EnforcedStyle: empty_lines_except_namespace
21
+ Layout/LineLength:
22
+ Max: 100
23
+ Layout/SpaceAroundMethodCallOperator:
24
+ Enabled: true
25
+ Lint/RaiseException:
26
+ Enabled: true
27
+ Lint/StructNewOverride:
28
+ Enabled: true
17
29
  Metrics/BlockLength:
18
30
  Exclude:
19
31
  - 'spec/**/**/*'
@@ -22,6 +34,8 @@ Metrics/ModuleLength:
22
34
  Enabled: false
23
35
  RSpec/MultipleExpectations:
24
36
  Enabled: false
37
+ Style/ArgumentsForwarding:
38
+ Enabled: false
25
39
  Style/Documentation:
26
40
  Enabled: false
27
41
  Style/ExpandPathArguments:
data/.travis.yml CHANGED
@@ -4,6 +4,7 @@ cache: bundler
4
4
  rvm:
5
5
  - 2.5
6
6
  - 2.6
7
+ - 2.7
7
8
  - ruby-head
8
9
  matrix:
9
10
  fast_finish: true
data/CHANGELOG.md CHANGED
@@ -6,6 +6,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.2.0] - 2021-07-19
10
+ ### Added
11
+ - Added Ruby 3.0 support
12
+
13
+ ## [1.1.0] - 2021-04-12
14
+ ### Added
15
+ - Added deterministic encryption
16
+ ### Changed
17
+ - Update configuration methods
18
+
19
+ ## [1.0.1] - 2020-07-03
20
+ ### Added
21
+ - Added Ruby 2.7 support
22
+
23
+ ## [1.0.1] - 2019-08-26
24
+ ### Added
25
+ - Key class for key generation
26
+ ### Changed
27
+ - Update initializer file
28
+
9
29
  ## [1.0.0] - 2019-08-24
10
30
  ### Added
11
31
  - Initial project version
data/Gemfile.lock CHANGED
@@ -1,123 +1,138 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-encryption (1.0.0)
4
+ lite-encryption (1.2.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actionpack (6.0.0)
11
- actionview (= 6.0.0)
12
- activesupport (= 6.0.0)
13
- rack (~> 2.0)
10
+ actionpack (6.1.4)
11
+ actionview (= 6.1.4)
12
+ activesupport (= 6.1.4)
13
+ rack (~> 2.0, >= 2.0.9)
14
14
  rack-test (>= 0.6.3)
15
15
  rails-dom-testing (~> 2.0)
16
16
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
- actionview (6.0.0)
18
- activesupport (= 6.0.0)
17
+ actionview (6.1.4)
18
+ activesupport (= 6.1.4)
19
19
  builder (~> 3.1)
20
20
  erubi (~> 1.4)
21
21
  rails-dom-testing (~> 2.0)
22
22
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
- activemodel (6.0.0)
24
- activesupport (= 6.0.0)
25
- activerecord (6.0.0)
26
- activemodel (= 6.0.0)
27
- activesupport (= 6.0.0)
28
- activesupport (6.0.0)
23
+ activemodel (6.1.4)
24
+ activesupport (= 6.1.4)
25
+ activerecord (6.1.4)
26
+ activemodel (= 6.1.4)
27
+ activesupport (= 6.1.4)
28
+ activesupport (6.1.4)
29
29
  concurrent-ruby (~> 1.0, >= 1.0.2)
30
- i18n (>= 0.7, < 2)
31
- minitest (~> 5.1)
32
- tzinfo (~> 1.1)
33
- zeitwerk (~> 2.1, >= 2.1.8)
34
- ast (2.4.0)
35
- builder (3.2.3)
30
+ i18n (>= 1.6, < 2)
31
+ minitest (>= 5.1)
32
+ tzinfo (~> 2.0)
33
+ zeitwerk (~> 2.3)
34
+ ast (2.4.2)
35
+ builder (3.2.4)
36
36
  colorize (0.8.1)
37
- concurrent-ruby (1.1.5)
38
- crass (1.0.4)
39
- database_cleaner (1.7.0)
40
- diff-lcs (1.3)
41
- erubi (1.8.0)
42
- fasterer (0.6.0)
37
+ concurrent-ruby (1.1.9)
38
+ crass (1.0.6)
39
+ database_cleaner (2.0.1)
40
+ database_cleaner-active_record (~> 2.0.0)
41
+ database_cleaner-active_record (2.0.1)
42
+ activerecord (>= 5.a)
43
+ database_cleaner-core (~> 2.0.0)
44
+ database_cleaner-core (2.0.1)
45
+ diff-lcs (1.4.4)
46
+ erubi (1.10.0)
47
+ fasterer (0.9.0)
43
48
  colorize (~> 0.7)
44
- ruby_parser (>= 3.13.0)
49
+ ruby_parser (>= 3.14.1)
45
50
  generator_spec (0.9.4)
46
51
  activesupport (>= 3.0.0)
47
52
  railties (>= 3.0.0)
48
- i18n (1.6.0)
53
+ i18n (1.8.10)
49
54
  concurrent-ruby (~> 1.0)
50
- jaro_winkler (1.5.3)
51
- loofah (2.2.3)
55
+ loofah (2.10.0)
52
56
  crass (~> 1.0.2)
53
57
  nokogiri (>= 1.5.9)
54
- method_source (0.9.2)
55
- mini_portile2 (2.4.0)
56
- minitest (5.11.3)
57
- nokogiri (1.10.4)
58
- mini_portile2 (~> 2.4.0)
59
- parallel (1.17.0)
60
- parser (2.6.3.0)
61
- ast (~> 2.4.0)
62
- rack (2.0.7)
58
+ method_source (1.0.0)
59
+ mini_portile2 (2.5.3)
60
+ minitest (5.14.4)
61
+ nokogiri (1.11.7)
62
+ mini_portile2 (~> 2.5.0)
63
+ racc (~> 1.4)
64
+ parallel (1.20.1)
65
+ parser (3.0.2.0)
66
+ ast (~> 2.4.1)
67
+ racc (1.5.2)
68
+ rack (2.2.3)
63
69
  rack-test (1.1.0)
64
70
  rack (>= 1.0, < 3)
65
71
  rails-dom-testing (2.0.3)
66
72
  activesupport (>= 4.2.0)
67
73
  nokogiri (>= 1.6)
68
- rails-html-sanitizer (1.2.0)
69
- loofah (~> 2.2, >= 2.2.2)
70
- railties (6.0.0)
71
- actionpack (= 6.0.0)
72
- activesupport (= 6.0.0)
74
+ rails-html-sanitizer (1.3.0)
75
+ loofah (~> 2.3)
76
+ railties (6.1.4)
77
+ actionpack (= 6.1.4)
78
+ activesupport (= 6.1.4)
73
79
  method_source
74
- rake (>= 0.8.7)
75
- thor (>= 0.20.3, < 2.0)
80
+ rake (>= 0.13)
81
+ thor (~> 1.0)
76
82
  rainbow (3.0.0)
77
- rake (12.3.3)
78
- rspec (3.8.0)
79
- rspec-core (~> 3.8.0)
80
- rspec-expectations (~> 3.8.0)
81
- rspec-mocks (~> 3.8.0)
82
- rspec-core (3.8.2)
83
- rspec-support (~> 3.8.0)
84
- rspec-expectations (3.8.4)
83
+ rake (13.0.6)
84
+ regexp_parser (2.1.1)
85
+ rexml (3.2.5)
86
+ rspec (3.10.0)
87
+ rspec-core (~> 3.10.0)
88
+ rspec-expectations (~> 3.10.0)
89
+ rspec-mocks (~> 3.10.0)
90
+ rspec-core (3.10.1)
91
+ rspec-support (~> 3.10.0)
92
+ rspec-expectations (3.10.1)
85
93
  diff-lcs (>= 1.2.0, < 2.0)
86
- rspec-support (~> 3.8.0)
87
- rspec-mocks (3.8.1)
94
+ rspec-support (~> 3.10.0)
95
+ rspec-mocks (3.10.2)
88
96
  diff-lcs (>= 1.2.0, < 2.0)
89
- rspec-support (~> 3.8.0)
90
- rspec-rails (3.8.2)
91
- actionpack (>= 3.0)
92
- activesupport (>= 3.0)
93
- railties (>= 3.0)
94
- rspec-core (~> 3.8.0)
95
- rspec-expectations (~> 3.8.0)
96
- rspec-mocks (~> 3.8.0)
97
- rspec-support (~> 3.8.0)
98
- rspec-support (3.8.2)
99
- rubocop (0.74.0)
100
- jaro_winkler (~> 1.5.1)
97
+ rspec-support (~> 3.10.0)
98
+ rspec-rails (5.0.1)
99
+ actionpack (>= 5.2)
100
+ activesupport (>= 5.2)
101
+ railties (>= 5.2)
102
+ rspec-core (~> 3.10)
103
+ rspec-expectations (~> 3.10)
104
+ rspec-mocks (~> 3.10)
105
+ rspec-support (~> 3.10)
106
+ rspec-support (3.10.2)
107
+ rubocop (1.18.3)
101
108
  parallel (~> 1.10)
102
- parser (>= 2.6)
109
+ parser (>= 3.0.0.0)
103
110
  rainbow (>= 2.2.2, < 4.0)
111
+ regexp_parser (>= 1.8, < 3.0)
112
+ rexml
113
+ rubocop-ast (>= 1.7.0, < 2.0)
104
114
  ruby-progressbar (~> 1.7)
105
- unicode-display_width (>= 1.4.0, < 1.7)
106
- rubocop-performance (1.4.1)
107
- rubocop (>= 0.71.0)
108
- rubocop-rspec (1.35.0)
109
- rubocop (>= 0.60.0)
110
- ruby-progressbar (1.10.1)
111
- ruby_parser (3.13.1)
112
- sexp_processor (~> 4.9)
113
- sexp_processor (4.12.1)
114
- sqlite3 (1.4.1)
115
- thor (0.20.3)
116
- thread_safe (0.3.6)
117
- tzinfo (1.2.5)
118
- thread_safe (~> 0.1)
119
- unicode-display_width (1.6.0)
120
- zeitwerk (2.1.9)
115
+ unicode-display_width (>= 1.4.0, < 3.0)
116
+ rubocop-ast (1.8.0)
117
+ parser (>= 3.0.1.1)
118
+ rubocop-performance (1.11.4)
119
+ rubocop (>= 1.7.0, < 2.0)
120
+ rubocop-ast (>= 0.4.0)
121
+ rubocop-rake (0.6.0)
122
+ rubocop (~> 1.0)
123
+ rubocop-rspec (2.4.0)
124
+ rubocop (~> 1.0)
125
+ rubocop-ast (>= 1.1.0)
126
+ ruby-progressbar (1.11.0)
127
+ ruby_parser (3.16.0)
128
+ sexp_processor (~> 4.15, >= 4.15.1)
129
+ sexp_processor (4.15.3)
130
+ sqlite3 (1.4.2)
131
+ thor (1.1.0)
132
+ tzinfo (2.0.4)
133
+ concurrent-ruby (~> 1.0)
134
+ unicode-display_width (2.0.0)
135
+ zeitwerk (2.4.2)
121
136
 
122
137
  PLATFORMS
123
138
  ruby
@@ -134,8 +149,9 @@ DEPENDENCIES
134
149
  rspec-rails
135
150
  rubocop
136
151
  rubocop-performance
152
+ rubocop-rake
137
153
  rubocop-rspec
138
154
  sqlite3
139
155
 
140
156
  BUNDLED WITH
141
- 2.0.1
157
+ 2.2.24
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/lite-encryption.svg)](http://badge.fury.io/rb/lite-encryption)
4
4
  [![Build Status](https://travis-ci.org/drexed/lite-encryption.svg?branch=master)](https://travis-ci.org/drexed/lite-encryption)
5
5
 
6
- Lite::Encryption is a ActiveSupport::MessageEncryptor wrapper library for encrypting and decrypting
7
- PORO objects and model attributes.
6
+ Lite::Encryption is a ActiveSupport::MessageEncryptor and OpenSSL::Cipher::Cipher wrapper libraries
7
+ for encrypting and decrypting PORO object values and model attributes.
8
8
 
9
9
  ## Installation
10
10
 
@@ -25,6 +25,8 @@ Or install it yourself as:
25
25
  ## Table of Contents
26
26
 
27
27
  * [Configurations](#configurations)
28
+ * [Key](#key)
29
+ * [Schemes](#schemes)
28
30
  * [Message](#message)
29
31
  * [Attribute](#attribute)
30
32
 
@@ -35,26 +37,47 @@ Or install it yourself as:
35
37
 
36
38
  ```ruby
37
39
  Lite::Encryption.configure do |config|
38
- config.secret_key_base = nil
39
- config.secret_key_salt = nil
40
+ config.encryption_iv = ENV['ENCRYPTION_IV']
41
+ config.encryption_key = ENV['ENCRYPTION_KEY']
42
+ config.encryption_salt = ENV['ENCRYPTION_SALT']
40
43
  end
41
44
  ```
42
45
 
43
- `secret_key_base` and `secret_key_salt` should be supplied via environment variables or a secret
44
- management system.
46
+ ## Key
47
+
48
+ Use the following to generate encryption values that you can then add to your
49
+ credentials or ENV variables.
50
+
51
+ ```ruby
52
+ Lite::Encryption::Key.generate_iv #=> "\x9C\x9F5<\xA5\x9B\x97\x97\xE2\x1DQ\x05"
53
+ Lite::Encryption::Key.generate_key #=> "y\x0Eo\xC1Gll\x05Qv\x11[\xE1vN8<\xAFxU_\xCB\xD7\x02)0\xC1#\x99\xDD4N"
54
+ Lite::Encryption::Key.generate_password #=> "289b4997f758b31693d3315679d9fa7c"
55
+ Lite::Encryption::Key.generate_salt #=> "\xD2\xBA\x9B\x81@e\x99\x8BN\a7\xC2\x95)f\x97k\xC9EM\xE0x\xFBO\x9BERBD\x85%n"
56
+ ```
57
+
58
+ ## Schemes
59
+
60
+ The non-deterministic message class is the wrapper class for `ActiveSupport::MessageEncryptor` so
61
+ you can pass it accepted options.
62
+
63
+ The deterministic message class is the wrapper class for `OpenSSL::Cipher::Cipher` so
64
+ you can pass it accepted options.
45
65
 
46
- To generate a `secret_key_base`, execute `bundle exec rails secret` in the terminal prompt.
47
- To generate a `secret_key_salt`, execute the following command in the Rails console prompt:
48
66
  ```ruby
49
- SecureRandom.random_bytes(
50
- ActiveSupport::MessageEncryptor.key_len
51
- )
67
+ Lite::Encryption::Schemes::NonDeterministic.encrypt('decrypted_text', purpose: 'sec-pur')
68
+ Lite::Encryption::Schemes::NonDeterministic.decrypt('==encrypted_text')
69
+
70
+ # - or -
71
+
72
+ service = Lite::Encryption::Deterministic.new
73
+
74
+ service.encrypt('decrypted_text')
75
+ service.decrypt('==encrypted_text')
52
76
  ```
53
77
 
54
78
  ## Message
55
79
 
56
- The message class is the wrapper class for `ActiveSupport::MessageEncryptor` so you can pass it
57
- accepted options.
80
+ The message class is the wrapper class for both schemes so you can pass it accepted options.
58
81
 
59
82
  ```ruby
60
83
  Lite::Encryption::Message.encrypt('decrypted_text', purpose: 'sec-pur')
@@ -66,6 +89,18 @@ service = Lite::Encryption::Message.new
66
89
 
67
90
  service.encrypt('decrypted_text', expires_in: 2.hours)
68
91
  service.decrypt('==encrypted_text')
92
+
93
+ # - or -
94
+
95
+ Lite::Encryption::Message.encrypt('decrypted_text', deterministic: true)
96
+ Lite::Encryption::Message.decrypt('==encrypted_text', deterministic: true)
97
+
98
+ # - or -
99
+
100
+ service = Lite::Encryption::Message.new
101
+
102
+ service.encrypt('decrypted_text', deterministic: true)
103
+ service.decrypt('==encrypted_text', deterministic: true)
69
104
  ```
70
105
 
71
106
  ## Attribute
@@ -79,6 +114,7 @@ encrypt and decrypt your values.
79
114
  class CreditCard < ActiveRecord::Base
80
115
  extend Lite::Encryption::Attribute
81
116
 
117
+ attr_encrypt :name, deterministic: true
82
118
  attr_encrypt :number, :cvv, purpose: 'payment-menthod'
83
119
 
84
120
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Lite::Encryption.configure do |config|
4
- config.secret_key_base = nil
5
- config.secret_key_salt = nil
4
+ config.encryption_iv = ENV['ENCRYPTION_IV']
5
+ config.encryption_key = ENV['ENCRYPTION_KEY']
6
+ config.encryption_salt = ENV['ENCRYPTION_SALT']
6
7
  end
@@ -1,6 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[version configuration message attribute].each do |filename|
3
+ %w[key_generator message_encryptor message_verifier].each do |filename|
4
+ require "active_support/#{filename}"
5
+ end
6
+
7
+ require 'lite/encryption/version'
8
+
9
+ %w[key configuration].each do |filename|
10
+ require "lite/encryption/#{filename}"
11
+ end
12
+
13
+ require 'lite/encryption/helpers/class_methods'
14
+
15
+ %w[deterministic non_deterministic].each do |filename|
16
+ require "lite/encryption/schemes/#{filename}"
17
+ end
18
+
19
+ %w[message attribute].each do |filename|
4
20
  require "lite/encryption/#{filename}"
5
21
  end
6
22
 
@@ -5,14 +5,13 @@ module Lite
5
5
 
6
6
  class Configuration
7
7
 
8
- attr_accessor :secret_key_base, :secret_key_salt
8
+ attr_accessor :encryption_iv, :encryption_key, :encryption_salt
9
9
 
10
- # rubocop:disable Metrics/LineLength
11
10
  def initialize
12
- @secret_key_base = 'b912e83c02b44122e31809a7435bc91e2e48c88742365aaccb07283eeb0379909e9aa09d64fc27cb5f20c3f072cd69aacd57518916799c00d41d94c06c916f5c'
13
- @secret_key_salt = "\xD5\x8C\xB6\x14\xAC\xC7-&\xAEu\xDDj\x80/\xDF\x15\xD1\xB2\x13\x04\x85\b\x8F\xC6ZQ`Z\xC7\xD4q\xDE"
11
+ @encryption_iv = Lite::Encryption::Key.generate_iv
12
+ @encryption_key = Lite::Encryption::Key.generate_key
13
+ @encryption_salt = Lite::Encryption::Key.generate_salt
14
14
  end
15
- # rubocop:enable Metrics/LineLength
16
15
 
17
16
  end
18
17
 
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Encryption
5
+ module Helpers
6
+ module ClassMethods
7
+
8
+ %i[decrypt encrypt].each do |name|
9
+ define_method(name) do |value, opts = {}|
10
+ klass = new
11
+ klass.send(name, value, opts)
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ module Lite
6
+ module Encryption
7
+ class Key
8
+
9
+ CIPHER = OpenSSL::Cipher.new('aes-256-gcm').freeze
10
+ LENGTHS = {
11
+ iv: CIPHER.iv_len,
12
+ key: CIPHER.key_len,
13
+ password: 16,
14
+ salt: CIPHER.key_len
15
+ }.freeze
16
+
17
+ class << self
18
+
19
+ def generate_iv
20
+ SecureRandom.random_bytes(LENGTHS[:iv])
21
+ end
22
+
23
+ def generate_key
24
+ generator = ActiveSupport::KeyGenerator.new(generate_password)
25
+ generator.generate_key(generate_salt, LENGTHS[:key])
26
+ end
27
+
28
+ def generate_password
29
+ SecureRandom.hex(LENGTHS[:password])
30
+ end
31
+
32
+ def generate_salt
33
+ SecureRandom.random_bytes(LENGTHS[:salt])
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -1,45 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- %w[key_generator message_encryptor message_verifier].each do |filename|
4
- require "active_support/#{filename}"
5
- end
6
-
7
3
  module Lite
8
4
  module Encryption
9
5
  class Message
10
6
 
11
- KEY ||= ActiveSupport::KeyGenerator.new(
12
- Lite::Encryption.configuration.secret_key_base
13
- ).generate_key(
14
- Lite::Encryption.configuration.secret_key_salt,
15
- ActiveSupport::MessageEncryptor.key_len
16
- ).freeze
17
-
18
- private_constant :KEY
19
-
20
- class << self
21
-
22
- %i[decrypt encrypt].each do |name|
23
- define_method(name) do |value, opts = {}|
24
- klass = new
25
- klass.send(name, value, opts)
26
- end
27
- end
28
-
29
- end
7
+ extend Lite::Encryption::Helpers::ClassMethods
30
8
 
31
9
  def decrypt(value, opts = {})
32
- encryptor.decrypt_and_verify(value, opts)
10
+ scheme = scheme_by_option(opts)
11
+ scheme.decrypt(value, **opts)
33
12
  end
34
13
 
35
14
  def encrypt(value, opts = {})
36
- encryptor.encrypt_and_sign(value, opts)
15
+ scheme = scheme_by_option(opts)
16
+ scheme.encrypt(value, **opts)
37
17
  end
38
18
 
39
19
  private
40
20
 
41
- def encryptor
42
- @encryptor ||= ActiveSupport::MessageEncryptor.new(KEY)
21
+ def deterministic_scheme
22
+ @deterministic_scheme ||= Lite::Encryption::Schemes::Deterministic.new
23
+ end
24
+
25
+ def non_deterministic_scheme
26
+ @non_deterministic_scheme ||= Lite::Encryption::Schemes::NonDeterministic.new
27
+ end
28
+
29
+ def scheme_by_option(options)
30
+ options.delete(:deterministic) ? deterministic_scheme : non_deterministic_scheme
43
31
  end
44
32
 
45
33
  end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Encryption
5
+ module Schemes
6
+ class Deterministic
7
+
8
+ extend Lite::Encryption::Helpers::ClassMethods
9
+
10
+ def decrypt(value, _opts = {})
11
+ decoded_value = Base64.strict_decode64(value)
12
+ crypt(:decrypt, decoded_value)
13
+ end
14
+
15
+ def encrypt(value, _opts = {})
16
+ encoded_value = crypt(:encrypt, value)
17
+ Base64.strict_encode64(encoded_value)
18
+ end
19
+
20
+ private
21
+
22
+ def cipher
23
+ @cipher ||= Lite::Encryption::Key::CIPHER.dup
24
+ end
25
+
26
+ def crypt(cipher_method, value)
27
+ cipher.send(cipher_method)
28
+ cipher.key = Lite::Encryption.configuration.encryption_salt
29
+ cipher.iv = Lite::Encryption.configuration.encryption_iv
30
+ cipher.update(value)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Encryption
5
+ module Schemes
6
+ class NonDeterministic
7
+
8
+ extend Lite::Encryption::Helpers::ClassMethods
9
+
10
+ def decrypt(value, opts = {})
11
+ cipher.decrypt_and_verify(value, **opts)
12
+ end
13
+
14
+ def encrypt(value, opts = {})
15
+ cipher.encrypt_and_sign(value, **opts)
16
+ end
17
+
18
+ private
19
+
20
+ def cipher
21
+ @cipher ||= ActiveSupport::MessageEncryptor.new(
22
+ Lite::Encryption.configuration.encryption_key,
23
+ cipher: Lite::Encryption::Key::CIPHER.name
24
+ )
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Encryption
5
5
 
6
- VERSION ||= '1.0.0'
6
+ VERSION = '1.2.0'
7
7
 
8
8
  end
9
9
  end
@@ -10,9 +10,9 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Juan Gomez']
11
11
  spec.email = %w[j.gomez@drexed.com]
12
12
 
13
- # rubocop:disable Metrics/LineLength
13
+ # rubocop:disable Layout/LineLength
14
14
  spec.summary = 'ActiveSupport::MessageEncryptor encryption wrapper for PORO objects and ActiveModel attributes'
15
- # rubocop:enable Metrics/LineLength
15
+ # rubocop:enable Layout/LineLength
16
16
  spec.homepage = 'http://drexed.github.io/lite-encryption'
17
17
  spec.license = 'MIT'
18
18
 
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  )
28
28
  else
29
29
  raise 'RubyGems 2.0 or newer is required to protect against ' \
30
- 'public gem pushes.'
30
+ 'public gem pushes.'
31
31
  end
32
32
 
33
33
  # Specify which files should be added to the gem when it is released.
@@ -51,6 +51,7 @@ Gem::Specification.new do |spec|
51
51
  spec.add_development_dependency 'rspec-rails'
52
52
  spec.add_development_dependency 'rubocop'
53
53
  spec.add_development_dependency 'rubocop-performance'
54
+ spec.add_development_dependency 'rubocop-rake'
54
55
  spec.add_development_dependency 'rubocop-rspec'
55
56
  spec.add_development_dependency 'sqlite3'
56
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-encryption
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-25 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rake
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: rubocop-rspec
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -192,7 +206,7 @@ dependencies:
192
206
  - - ">="
193
207
  - !ruby/object:Gem::Version
194
208
  version: '0'
195
- description:
209
+ description:
196
210
  email:
197
211
  - j.gomez@drexed.com
198
212
  executables: []
@@ -219,14 +233,18 @@ files:
219
233
  - lib/lite/encryption.rb
220
234
  - lib/lite/encryption/attribute.rb
221
235
  - lib/lite/encryption/configuration.rb
236
+ - lib/lite/encryption/helpers/class_methods.rb
237
+ - lib/lite/encryption/key.rb
222
238
  - lib/lite/encryption/message.rb
239
+ - lib/lite/encryption/schemes/deterministic.rb
240
+ - lib/lite/encryption/schemes/non_deterministic.rb
223
241
  - lib/lite/encryption/version.rb
224
242
  - lite-encryption.gemspec
225
243
  homepage: http://drexed.github.io/lite-encryption
226
244
  licenses:
227
245
  - MIT
228
246
  metadata: {}
229
- post_install_message:
247
+ post_install_message:
230
248
  rdoc_options: []
231
249
  require_paths:
232
250
  - lib
@@ -241,8 +259,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
259
  - !ruby/object:Gem::Version
242
260
  version: '0'
243
261
  requirements: []
244
- rubygems_version: 3.0.4
245
- signing_key:
262
+ rubygems_version: 3.2.24
263
+ signing_key:
246
264
  specification_version: 4
247
265
  summary: ActiveSupport::MessageEncryptor encryption wrapper for PORO objects and ActiveModel
248
266
  attributes