lite-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.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +88 -77
- data/README.md +46 -9
- data/lib/generators/lite/encryption/templates/install.rb +3 -2
- data/lib/lite/encryption.rb +17 -1
- data/lib/lite/encryption/configuration.rb +4 -3
- data/lib/lite/encryption/helpers/class_methods.rb +18 -0
- data/lib/lite/encryption/key.rb +19 -5
- data/lib/lite/encryption/message.rb +15 -27
- data/lib/lite/encryption/schemes/deterministic.rb +36 -0
- data/lib/lite/encryption/schemes/non_deterministic.rb +30 -0
- data/lib/lite/encryption/version.rb +1 -1
- data/lite-encryption.gemspec +1 -0
- metadata +20 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cdda189309ae7d6af8fac9257314a5e57ffb942f6f9443a3d25dc13bf96d4ffe
|
|
4
|
+
data.tar.gz: ab1701787569d474a5d9107bc13522b71ea6339cf2d8542e3089356098d0321d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5017b7033e5d2939ce0fb02a534b7dcaf4c4cd2a069b9a6b0cca29a4838757d84d8a9f376f9ffc62d127b941c5082beb59cf5819a6a88f27ff7fef34fc3b2076
|
|
7
|
+
data.tar.gz: 65acf87b0fe976d86e9e9399e53cef33d1fca10f799d9f41819e8d4a89ee3f20939247b003248ed020b92111ac232b22c4b6cafa0525d6bf5cb22042c7424343
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [1.1.0] - 2021-04-12
|
|
10
|
+
### Added
|
|
11
|
+
- Added deterministic encryption
|
|
12
|
+
### Changed
|
|
13
|
+
- Update configuration methods
|
|
14
|
+
|
|
9
15
|
## [1.0.1] - 2020-07-03
|
|
10
16
|
### Added
|
|
11
17
|
- Added Ruby 2.7 support
|
data/Gemfile.lock
CHANGED
|
@@ -1,63 +1,70 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
lite-encryption (1.0
|
|
4
|
+
lite-encryption (1.1.0)
|
|
5
5
|
activesupport
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
actionpack (6.
|
|
11
|
-
actionview (= 6.
|
|
12
|
-
activesupport (= 6.
|
|
13
|
-
rack (~> 2.0, >= 2.0.
|
|
10
|
+
actionpack (6.1.3.1)
|
|
11
|
+
actionview (= 6.1.3.1)
|
|
12
|
+
activesupport (= 6.1.3.1)
|
|
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.
|
|
18
|
-
activesupport (= 6.
|
|
17
|
+
actionview (6.1.3.1)
|
|
18
|
+
activesupport (= 6.1.3.1)
|
|
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.
|
|
24
|
-
activesupport (= 6.
|
|
25
|
-
activerecord (6.
|
|
26
|
-
activemodel (= 6.
|
|
27
|
-
activesupport (= 6.
|
|
28
|
-
activesupport (6.
|
|
23
|
+
activemodel (6.1.3.1)
|
|
24
|
+
activesupport (= 6.1.3.1)
|
|
25
|
+
activerecord (6.1.3.1)
|
|
26
|
+
activemodel (= 6.1.3.1)
|
|
27
|
+
activesupport (= 6.1.3.1)
|
|
28
|
+
activesupport (6.1.3.1)
|
|
29
29
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
30
|
-
i18n (>=
|
|
31
|
-
minitest (
|
|
32
|
-
tzinfo (~>
|
|
33
|
-
zeitwerk (~> 2.
|
|
34
|
-
ast (2.4.
|
|
30
|
+
i18n (>= 1.6, < 2)
|
|
31
|
+
minitest (>= 5.1)
|
|
32
|
+
tzinfo (~> 2.0)
|
|
33
|
+
zeitwerk (~> 2.3)
|
|
34
|
+
ast (2.4.2)
|
|
35
35
|
builder (3.2.4)
|
|
36
36
|
colorize (0.8.1)
|
|
37
|
-
concurrent-ruby (1.1.
|
|
37
|
+
concurrent-ruby (1.1.8)
|
|
38
38
|
crass (1.0.6)
|
|
39
|
-
database_cleaner (
|
|
39
|
+
database_cleaner (2.0.1)
|
|
40
|
+
database_cleaner-active_record (~> 2.0.0)
|
|
41
|
+
database_cleaner-active_record (2.0.0)
|
|
42
|
+
activerecord (>= 5.a)
|
|
43
|
+
database_cleaner-core (~> 2.0.0)
|
|
44
|
+
database_cleaner-core (2.0.1)
|
|
40
45
|
diff-lcs (1.4.4)
|
|
41
|
-
erubi (1.
|
|
42
|
-
fasterer (0.
|
|
46
|
+
erubi (1.10.0)
|
|
47
|
+
fasterer (0.9.0)
|
|
43
48
|
colorize (~> 0.7)
|
|
44
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.8.
|
|
53
|
+
i18n (1.8.10)
|
|
49
54
|
concurrent-ruby (~> 1.0)
|
|
50
|
-
loofah (2.
|
|
55
|
+
loofah (2.9.1)
|
|
51
56
|
crass (~> 1.0.2)
|
|
52
57
|
nokogiri (>= 1.5.9)
|
|
53
58
|
method_source (1.0.0)
|
|
54
|
-
mini_portile2 (2.
|
|
55
|
-
minitest (5.14.
|
|
56
|
-
nokogiri (1.
|
|
57
|
-
mini_portile2 (~> 2.
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
mini_portile2 (2.5.0)
|
|
60
|
+
minitest (5.14.4)
|
|
61
|
+
nokogiri (1.11.3)
|
|
62
|
+
mini_portile2 (~> 2.5.0)
|
|
63
|
+
racc (~> 1.4)
|
|
64
|
+
parallel (1.20.1)
|
|
65
|
+
parser (3.0.1.0)
|
|
60
66
|
ast (~> 2.4.1)
|
|
67
|
+
racc (1.5.2)
|
|
61
68
|
rack (2.2.3)
|
|
62
69
|
rack-test (1.1.0)
|
|
63
70
|
rack (>= 1.0, < 3)
|
|
@@ -66,63 +73,66 @@ GEM
|
|
|
66
73
|
nokogiri (>= 1.6)
|
|
67
74
|
rails-html-sanitizer (1.3.0)
|
|
68
75
|
loofah (~> 2.3)
|
|
69
|
-
railties (6.
|
|
70
|
-
actionpack (= 6.
|
|
71
|
-
activesupport (= 6.
|
|
76
|
+
railties (6.1.3.1)
|
|
77
|
+
actionpack (= 6.1.3.1)
|
|
78
|
+
activesupport (= 6.1.3.1)
|
|
72
79
|
method_source
|
|
73
80
|
rake (>= 0.8.7)
|
|
74
|
-
thor (
|
|
81
|
+
thor (~> 1.0)
|
|
75
82
|
rainbow (3.0.0)
|
|
76
|
-
rake (13.0.
|
|
77
|
-
regexp_parser (1.
|
|
78
|
-
rexml (3.2.
|
|
79
|
-
rspec (3.
|
|
80
|
-
rspec-core (~> 3.
|
|
81
|
-
rspec-expectations (~> 3.
|
|
82
|
-
rspec-mocks (~> 3.
|
|
83
|
-
rspec-core (3.
|
|
84
|
-
rspec-support (~> 3.
|
|
85
|
-
rspec-expectations (3.
|
|
83
|
+
rake (13.0.3)
|
|
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)
|
|
86
93
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
87
|
-
rspec-support (~> 3.
|
|
88
|
-
rspec-mocks (3.
|
|
94
|
+
rspec-support (~> 3.10.0)
|
|
95
|
+
rspec-mocks (3.10.2)
|
|
89
96
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
90
|
-
rspec-support (~> 3.
|
|
91
|
-
rspec-rails (
|
|
92
|
-
actionpack (>=
|
|
93
|
-
activesupport (>=
|
|
94
|
-
railties (>=
|
|
95
|
-
rspec-core (~> 3.
|
|
96
|
-
rspec-expectations (~> 3.
|
|
97
|
-
rspec-mocks (~> 3.
|
|
98
|
-
rspec-support (~> 3.
|
|
99
|
-
rspec-support (3.
|
|
100
|
-
rubocop (
|
|
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.12.1)
|
|
101
108
|
parallel (~> 1.10)
|
|
102
|
-
parser (>=
|
|
109
|
+
parser (>= 3.0.0.0)
|
|
103
110
|
rainbow (>= 2.2.2, < 4.0)
|
|
104
|
-
regexp_parser (>= 1.
|
|
111
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
105
112
|
rexml
|
|
106
|
-
rubocop-ast (>=
|
|
113
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
|
107
114
|
ruby-progressbar (~> 1.7)
|
|
108
|
-
unicode-display_width (>= 1.4.0, <
|
|
109
|
-
rubocop-ast (
|
|
110
|
-
parser (>= 2.7.
|
|
111
|
-
rubocop-performance (1.
|
|
112
|
-
rubocop (>= 0.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
116
|
+
rubocop-ast (1.4.1)
|
|
117
|
+
parser (>= 2.7.1.5)
|
|
118
|
+
rubocop-performance (1.10.2)
|
|
119
|
+
rubocop (>= 0.90.0, < 2.0)
|
|
120
|
+
rubocop-ast (>= 0.4.0)
|
|
121
|
+
rubocop-rake (0.5.1)
|
|
122
|
+
rubocop
|
|
123
|
+
rubocop-rspec (2.2.0)
|
|
124
|
+
rubocop (~> 1.0)
|
|
125
|
+
rubocop-ast (>= 1.1.0)
|
|
126
|
+
ruby-progressbar (1.11.0)
|
|
127
|
+
ruby_parser (3.15.1)
|
|
117
128
|
sexp_processor (~> 4.9)
|
|
118
|
-
sexp_processor (4.15.
|
|
129
|
+
sexp_processor (4.15.2)
|
|
119
130
|
sqlite3 (1.4.2)
|
|
120
|
-
thor (1.0
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
zeitwerk (2.3.1)
|
|
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)
|
|
126
136
|
|
|
127
137
|
PLATFORMS
|
|
128
138
|
ruby
|
|
@@ -139,8 +149,9 @@ DEPENDENCIES
|
|
|
139
149
|
rspec-rails
|
|
140
150
|
rubocop
|
|
141
151
|
rubocop-performance
|
|
152
|
+
rubocop-rake
|
|
142
153
|
rubocop-rspec
|
|
143
154
|
sqlite3
|
|
144
155
|
|
|
145
156
|
BUNDLED WITH
|
|
146
|
-
2.
|
|
157
|
+
2.2.16
|
data/README.md
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
[](http://badge.fury.io/rb/lite-encryption)
|
|
4
4
|
[](https://travis-ci.org/drexed/lite-encryption)
|
|
5
5
|
|
|
6
|
-
Lite::Encryption is a ActiveSupport::MessageEncryptor
|
|
7
|
-
PORO
|
|
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
|
|
|
@@ -26,6 +26,7 @@ Or install it yourself as:
|
|
|
26
26
|
|
|
27
27
|
* [Configurations](#configurations)
|
|
28
28
|
* [Key](#key)
|
|
29
|
+
* [Schemes](#schemes)
|
|
29
30
|
* [Message](#message)
|
|
30
31
|
* [Attribute](#attribute)
|
|
31
32
|
|
|
@@ -36,24 +37,47 @@ Or install it yourself as:
|
|
|
36
37
|
|
|
37
38
|
```ruby
|
|
38
39
|
Lite::Encryption.configure do |config|
|
|
39
|
-
config.
|
|
40
|
-
config.
|
|
40
|
+
config.encryption_iv = ENV['ENCRYPTION_IV']
|
|
41
|
+
config.encryption_key = ENV['ENCRYPTION_KEY']
|
|
42
|
+
config.encryption_salt = ENV['ENCRYPTION_SALT']
|
|
41
43
|
end
|
|
42
44
|
```
|
|
43
45
|
|
|
44
46
|
## Key
|
|
45
47
|
|
|
46
|
-
Use the following to generate
|
|
48
|
+
Use the following to generate encryption values that you can then add to your
|
|
49
|
+
credentials or ENV variables.
|
|
47
50
|
|
|
48
51
|
```ruby
|
|
49
|
-
Lite::Encryption::Key.
|
|
50
|
-
Lite::Encryption::Key.
|
|
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.
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
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')
|
|
51
76
|
```
|
|
52
77
|
|
|
53
78
|
## Message
|
|
54
79
|
|
|
55
|
-
The message class is the wrapper class for
|
|
56
|
-
accepted options.
|
|
80
|
+
The message class is the wrapper class for both schemes so you can pass it accepted options.
|
|
57
81
|
|
|
58
82
|
```ruby
|
|
59
83
|
Lite::Encryption::Message.encrypt('decrypted_text', purpose: 'sec-pur')
|
|
@@ -65,6 +89,18 @@ service = Lite::Encryption::Message.new
|
|
|
65
89
|
|
|
66
90
|
service.encrypt('decrypted_text', expires_in: 2.hours)
|
|
67
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)
|
|
68
104
|
```
|
|
69
105
|
|
|
70
106
|
## Attribute
|
|
@@ -78,6 +114,7 @@ encrypt and decrypt your values.
|
|
|
78
114
|
class CreditCard < ActiveRecord::Base
|
|
79
115
|
extend Lite::Encryption::Attribute
|
|
80
116
|
|
|
117
|
+
attr_encrypt :name, deterministic: true
|
|
81
118
|
attr_encrypt :number, :cvv, purpose: 'payment-menthod'
|
|
82
119
|
|
|
83
120
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
Lite::Encryption.configure do |config|
|
|
4
|
-
config.
|
|
5
|
-
config.
|
|
4
|
+
config.encryption_iv = ENV['ENCRYPTION_IV']
|
|
5
|
+
config.encryption_key = ENV['ENCRYPTION_KEY']
|
|
6
|
+
config.encryption_salt = ENV['ENCRYPTION_SALT']
|
|
6
7
|
end
|
data/lib/lite/encryption.rb
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
%w[
|
|
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,11 +5,12 @@ module Lite
|
|
|
5
5
|
|
|
6
6
|
class Configuration
|
|
7
7
|
|
|
8
|
-
attr_accessor :
|
|
8
|
+
attr_accessor :encryption_iv, :encryption_key, :encryption_salt
|
|
9
9
|
|
|
10
10
|
def initialize
|
|
11
|
-
@
|
|
12
|
-
@
|
|
11
|
+
@encryption_iv = Lite::Encryption::Key.generate_iv
|
|
12
|
+
@encryption_key = Lite::Encryption::Key.generate_key
|
|
13
|
+
@encryption_salt = Lite::Encryption::Key.generate_salt
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
end
|
|
@@ -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
|
data/lib/lite/encryption/key.rb
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'active_support/message_encryptor'
|
|
4
3
|
require 'securerandom'
|
|
5
4
|
|
|
6
5
|
module Lite
|
|
7
6
|
module Encryption
|
|
8
7
|
class Key
|
|
9
8
|
|
|
10
|
-
|
|
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
|
|
11
16
|
|
|
12
17
|
class << self
|
|
13
18
|
|
|
14
|
-
def
|
|
15
|
-
SecureRandom.
|
|
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])
|
|
16
30
|
end
|
|
17
31
|
|
|
18
32
|
def generate_salt
|
|
19
|
-
SecureRandom.random_bytes(
|
|
33
|
+
SecureRandom.random_bytes(LENGTHS[:salt])
|
|
20
34
|
end
|
|
21
35
|
|
|
22
36
|
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
|
-
|
|
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
|
-
|
|
10
|
+
scheme = scheme_by_option(opts)
|
|
11
|
+
scheme.decrypt(value, **opts)
|
|
33
12
|
end
|
|
34
13
|
|
|
35
14
|
def encrypt(value, opts = {})
|
|
36
|
-
|
|
15
|
+
scheme = scheme_by_option(opts)
|
|
16
|
+
scheme.encrypt(value, **opts)
|
|
37
17
|
end
|
|
38
18
|
|
|
39
19
|
private
|
|
40
20
|
|
|
41
|
-
def
|
|
42
|
-
@
|
|
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
|
data/lite-encryption.gemspec
CHANGED
|
@@ -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
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Juan Gomez
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-04-12 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
|
|
@@ -219,8 +233,11 @@ 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
|
|
222
237
|
- lib/lite/encryption/key.rb
|
|
223
238
|
- lib/lite/encryption/message.rb
|
|
239
|
+
- lib/lite/encryption/schemes/deterministic.rb
|
|
240
|
+
- lib/lite/encryption/schemes/non_deterministic.rb
|
|
224
241
|
- lib/lite/encryption/version.rb
|
|
225
242
|
- lite-encryption.gemspec
|
|
226
243
|
homepage: http://drexed.github.io/lite-encryption
|
|
@@ -242,7 +259,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
242
259
|
- !ruby/object:Gem::Version
|
|
243
260
|
version: '0'
|
|
244
261
|
requirements: []
|
|
245
|
-
rubygems_version: 3.
|
|
262
|
+
rubygems_version: 3.2.16
|
|
246
263
|
signing_key:
|
|
247
264
|
specification_version: 4
|
|
248
265
|
summary: ActiveSupport::MessageEncryptor encryption wrapper for PORO objects and ActiveModel
|