lite-encryption 1.0.2 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -1
- data/CHANGELOG.md +18 -0
- data/Gemfile.lock +90 -79
- data/README.md +46 -9
- data/lib/generators/lite/encryption/install_generator.rb +0 -2
- data/lib/generators/lite/encryption/templates/install.rb +3 -2
- data/lib/lite/encryption.rb +14 -3
- data/lib/lite/encryption/configuration.rb +4 -3
- data/lib/lite/encryption/helpers/class_methods.rb +18 -0
- data/lib/lite/encryption/key.rb +21 -6
- 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 +2 -1
- 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: 5c02f2ab70f1ff0625a889b54aa2050a034ff8a722a76b2d98ac1083e0a6c7f8
|
4
|
+
data.tar.gz: 4944034782b3ee546810aaf4f034ecaee1150dbd5885706bf61f19415309c612
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed5a4bb88a067f3323bc67f1cd3a8e0295b1546ca1d27958b394e08711ace80e2a48feb5122993096b7741422dbc4b0aa54e7d3ed7fdaf1e5ad12eccc9e21afe
|
7
|
+
data.tar.gz: ebba2e5005663b2d0fbbb16b01c655eb28c28491239fef3d524d845e64a5c3c32582eca2ffd7ee902e1640676b2560339ad9d20b95237a710befb1f80f83b465
|
data/.rubocop.yml
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
require:
|
2
2
|
- rubocop-performance
|
3
|
+
- rubocop-rake
|
3
4
|
- rubocop-rspec
|
4
5
|
AllCops:
|
5
|
-
TargetRubyVersion:
|
6
|
+
TargetRubyVersion: 3.0
|
6
7
|
NewCops: enable
|
7
8
|
DisplayCopNames: true
|
8
9
|
DisplayStyleGuide: true
|
10
|
+
Gemspec/RequiredRubyVersion:
|
11
|
+
Enabled: false
|
9
12
|
Layout/EmptyLinesAroundAttributeAccessor:
|
10
13
|
Enabled: true
|
11
14
|
Layout/EmptyLinesAroundBlockBody:
|
@@ -31,6 +34,8 @@ Metrics/ModuleLength:
|
|
31
34
|
Enabled: false
|
32
35
|
RSpec/MultipleExpectations:
|
33
36
|
Enabled: false
|
37
|
+
Style/ArgumentsForwarding:
|
38
|
+
Enabled: false
|
34
39
|
Style/Documentation:
|
35
40
|
Enabled: false
|
36
41
|
Style/ExpandPathArguments:
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.2.2] - 2021-07-21
|
10
|
+
### Changed
|
11
|
+
- Improved setup
|
12
|
+
|
13
|
+
## [1.2.1] - 2021-07-21
|
14
|
+
### Changed
|
15
|
+
- Improved Railtie support
|
16
|
+
|
17
|
+
## [1.2.0] - 2021-07-19
|
18
|
+
### Added
|
19
|
+
- Added Ruby 3.0 support
|
20
|
+
|
21
|
+
## [1.1.0] - 2021-04-12
|
22
|
+
### Added
|
23
|
+
- Added deterministic encryption
|
24
|
+
### Changed
|
25
|
+
- Update configuration methods
|
26
|
+
|
9
27
|
## [1.0.1] - 2020-07-03
|
10
28
|
### Added
|
11
29
|
- 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.
|
4
|
+
lite-encryption (1.2.2)
|
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.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.
|
18
|
-
activesupport (= 6.
|
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.
|
24
|
-
activesupport (= 6.
|
25
|
-
activerecord (6.
|
26
|
-
activemodel (= 6.
|
27
|
-
activesupport (= 6.
|
28
|
-
activesupport (6.
|
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 (>=
|
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.9)
|
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.1)
|
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.10.0)
|
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.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)
|
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.4)
|
77
|
+
actionpack (= 6.1.4)
|
78
|
+
activesupport (= 6.1.4)
|
72
79
|
method_source
|
73
|
-
rake (>= 0.
|
74
|
-
thor (
|
80
|
+
rake (>= 0.13)
|
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.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)
|
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.18.3)
|
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.7.0, < 2.0)
|
107
114
|
ruby-progressbar (~> 1.7)
|
108
|
-
unicode-display_width (>= 1.4.0, <
|
109
|
-
rubocop-ast (
|
110
|
-
parser (>=
|
111
|
-
rubocop-performance (1.
|
112
|
-
rubocop (>=
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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)
|
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.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
|
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,7 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require
|
3
|
+
unless defined?(ActiveSupport)
|
4
|
+
require 'active_support/key_generator'
|
5
|
+
require 'active_support/message_encryptor'
|
6
|
+
require 'active_support/message_verifier'
|
5
7
|
end
|
6
8
|
|
7
|
-
require 'generators/lite/encryption/install_generator'
|
9
|
+
require 'generators/lite/encryption/install_generator' if defined?(Rails::Generators)
|
10
|
+
|
11
|
+
require 'lite/encryption/version'
|
12
|
+
require 'lite/encryption/key'
|
13
|
+
require 'lite/encryption/configuration'
|
14
|
+
require 'lite/encryption/helpers/class_methods'
|
15
|
+
require 'lite/encryption/schemes/deterministic'
|
16
|
+
require 'lite/encryption/schemes/non_deterministic'
|
17
|
+
require 'lite/encryption/message'
|
18
|
+
require 'lite/encryption/attribute'
|
@@ -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,37 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
require 'securerandom'
|
3
|
+
require 'openssl' unless defined?(OpenSSL)
|
4
|
+
require 'securerandom' unless defined?(SecureRandom)
|
5
5
|
|
6
6
|
module Lite
|
7
7
|
module Encryption
|
8
8
|
class Key
|
9
9
|
|
10
|
-
|
10
|
+
CIPHER = OpenSSL::Cipher.new('aes-256-gcm').freeze
|
11
|
+
LENGTHS = {
|
12
|
+
iv: CIPHER.iv_len,
|
13
|
+
key: CIPHER.key_len,
|
14
|
+
password: 16,
|
15
|
+
salt: CIPHER.key_len
|
16
|
+
}.freeze
|
11
17
|
|
12
18
|
class << self
|
13
19
|
|
14
|
-
def
|
15
|
-
SecureRandom.
|
20
|
+
def generate_iv
|
21
|
+
SecureRandom.random_bytes(LENGTHS[:iv])
|
22
|
+
end
|
23
|
+
|
24
|
+
def generate_key
|
25
|
+
generator = ActiveSupport::KeyGenerator.new(generate_password)
|
26
|
+
generator.generate_key(generate_salt, LENGTHS[:key])
|
27
|
+
end
|
28
|
+
|
29
|
+
def generate_password
|
30
|
+
SecureRandom.hex(LENGTHS[:password])
|
16
31
|
end
|
17
32
|
|
18
33
|
def generate_salt
|
19
|
-
SecureRandom.random_bytes(
|
34
|
+
SecureRandom.random_bytes(LENGTHS[:salt])
|
20
35
|
end
|
21
36
|
|
22
37
|
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
@@ -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
|
-
|
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.
|
4
|
+
version: 1.2.2
|
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-07-22 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.24
|
246
263
|
signing_key:
|
247
264
|
specification_version: 4
|
248
265
|
summary: ActiveSupport::MessageEncryptor encryption wrapper for PORO objects and ActiveModel
|