master-crypt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 30c1cf54ea583219381fdbed0e73f77f1f4b7168fe7bf5e3a568832d50be208f
4
+ data.tar.gz: 8add2b4b02dc45935f7317561b09e49a1e1bca477634a0bf3672ffaf8fc80a4c
5
+ SHA512:
6
+ metadata.gz: b9b16ea951a97dd3577962c54303ec46ab1e3c9df2bd890408a925a69f65f71a555139facefca207c32f87d82bf8a4984e20711a95c0de807fa085c2a1ec9dca
7
+ data.tar.gz: d94abe5413f90b4a7cac2fcf875f834d6066904e0b2b36f3b3991a9123d89be19a19bd6b8484962a6ecf082b3c84bbab45bb06a825287498c12cc9aa41fdbd26
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # master-crypt
2
+
3
+ ## Development
4
+ ```sh
5
+ bundle install
6
+ ```
7
+
8
+ ### Specs
9
+ Run a
10
+ ```sh
11
+
12
+ ```
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "master_crypt"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euxo pipefail
3
+
4
+ bundle install
5
+
6
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,65 @@
1
+ require "master_crypt/version"
2
+ require "rbnacl"
3
+ require "base64"
4
+ require "digest"
5
+
6
+ module MasterCrypt
7
+ class << self
8
+ def encrypt(plaintext, secrets)
9
+ raise "Secrets must not be blank" unless secrets.select(&:empty?).empty?
10
+ raise "At least 1 secret is required" if !secrets.is_a?(Array) || secrets.empty?
11
+
12
+ random_key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
13
+ # encrypt data with random_key
14
+ box = RbNaCl::SimpleBox.from_secret_key(random_key)
15
+
16
+ encrypted_data = box.encrypt(plaintext)
17
+ encrypted_data64 = Base64.strict_encode64(encrypted_data)
18
+
19
+ # encrypt random_kets with each secret
20
+ encrypted_random_keys64 = secrets.collect do |secret|
21
+ key = generate_key_from_secret(secret)
22
+ box = RbNaCl::SimpleBox.from_secret_key(key)
23
+
24
+ encrypted_random_key = box.encrypt(random_key)
25
+ encrypted_random_key64 = Base64.strict_encode64(encrypted_random_key)
26
+ encrypted_random_key64
27
+ end
28
+ # encrypted_data|encrypted_random_key1:encrypted_random_key2:...
29
+ Base64.strict_encode64(encrypted_data64 + "|" + encrypted_random_keys64.join(":"))
30
+ end
31
+
32
+ def decrypt(encrypted_data, secret)
33
+ encrypted_data64, encrypted_random_keys64_joined = Base64.strict_decode64(encrypted_data).split("|", 2)
34
+
35
+ encrypted_random_keys64 = encrypted_random_keys64_joined.split(":")
36
+ key = find_key(encrypted_random_keys64, secret)
37
+
38
+ box = RbNaCl::SimpleBox.from_secret_key(key)
39
+
40
+ ciphertext = Base64.strict_decode64(encrypted_data64)
41
+ box.decrypt(ciphertext).force_encoding(Encoding::UTF_8)
42
+ end
43
+
44
+ private
45
+
46
+ def find_key(encrypted_random_keys64, secret)
47
+ secret_key = generate_key_from_secret(secret)
48
+ box = RbNaCl::SimpleBox.from_secret_key(secret_key)
49
+
50
+ encrypted_random_keys64.each do |encrypted_random_key64|
51
+ encrypted_random_key = Base64.strict_decode64(encrypted_random_key64)
52
+ begin
53
+ random_key = box.decrypt(encrypted_random_key)
54
+ return random_key
55
+ rescue RbNaCl::CryptoError
56
+ end
57
+ end
58
+ raise "No valid key for '#{secret}'"
59
+ end
60
+
61
+ def generate_key_from_secret(secret)
62
+ Digest::SHA2.hexdigest(secret)[0...32].force_encoding(Encoding::BINARY)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module MasterCrypt
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: master-crypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cian McElhinney
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: standardrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-doc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rbnacl
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '='
144
+ - !ruby/object:Gem::Version
145
+ version: 7.1.1
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '='
151
+ - !ruby/object:Gem::Version
152
+ version: 7.1.1
153
+ description: Allows for encrypting a single piece of data with multiple keys which
154
+ can each be used to decrypt it
155
+ email:
156
+ executables:
157
+ - setup
158
+ - console
159
+ extensions: []
160
+ extra_rdoc_files: []
161
+ files:
162
+ - README.md
163
+ - bin/console
164
+ - bin/setup
165
+ - lib/master_crypt.rb
166
+ - lib/master_crypt/version.rb
167
+ homepage: https://github.com/cianmce/master-crypt
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubygems_version: 3.1.4
187
+ signing_key:
188
+ specification_version: 4
189
+ summary: Encrypting data with multiple keys
190
+ test_files: []