symmetric-encryption 3.7.2 → 3.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +65 -83
- data/Rakefile +4 -4
- data/lib/rails/generators/symmetric_encryption/config/config_generator.rb +3 -3
- data/lib/rails/generators/symmetric_encryption/heroku_config/heroku_config_generator.rb +3 -3
- data/lib/rails/generators/symmetric_encryption/new_keys/new_keys_generator.rb +2 -2
- data/lib/symmetric_encryption.rb +7 -1
- data/lib/symmetric_encryption/cipher.rb +180 -50
- data/lib/symmetric_encryption/coerce.rb +75 -0
- data/lib/symmetric_encryption/config.rb +88 -0
- data/lib/symmetric_encryption/extensions/active_record/base.rb +2 -2
- data/lib/symmetric_encryption/extensions/mongoid/encrypted.rb +2 -2
- data/lib/symmetric_encryption/generator.rb +5 -1
- data/lib/symmetric_encryption/railtie.rb +3 -3
- data/lib/symmetric_encryption/railties/symmetric_encryption.rake +6 -6
- data/lib/symmetric_encryption/railties/symmetric_encryption_validator.rb +1 -1
- data/lib/symmetric_encryption/reader.rb +16 -14
- data/lib/symmetric_encryption/symmetric_encryption.rb +30 -285
- data/lib/symmetric_encryption/version.rb +1 -1
- data/lib/symmetric_encryption/writer.rb +13 -13
- data/test/active_record_test.rb +126 -73
- data/test/cipher_test.rb +42 -42
- data/test/mongo_mapper_test.rb +171 -114
- data/test/mongoid_test.rb +173 -115
- data/test/reader_test.rb +63 -63
- data/test/symmetric_encryption_test.rb +81 -80
- data/test/test_db.sqlite3 +0 -0
- data/test/test_helper.rb +1 -2
- data/test/writer_test.rb +20 -20
- metadata +13 -13
- data/lib/_test_empty +0 -0
data/test/reader_test.rb
CHANGED
@@ -4,17 +4,17 @@ require 'stringio'
|
|
4
4
|
# Unit Test for SymmetricEncrypted::ReaderStream
|
5
5
|
#
|
6
6
|
class ReaderTest < Minitest::Test
|
7
|
-
|
8
|
-
|
9
|
-
@data
|
7
|
+
describe SymmetricEncryption::Reader do
|
8
|
+
before do
|
9
|
+
@data = [
|
10
10
|
"Hello World\n",
|
11
11
|
"Keep this secret\n",
|
12
12
|
"And keep going even further and further..."
|
13
13
|
]
|
14
|
-
@data_str
|
15
|
-
@data_len
|
14
|
+
@data_str = @data.inject('') { |sum, str| sum << str }
|
15
|
+
@data_len = @data_str.length
|
16
16
|
# Use Cipher 0 since it does not always include a header
|
17
|
-
@cipher
|
17
|
+
@cipher = SymmetricEncryption.cipher(0)
|
18
18
|
@data_encrypted_without_header = @cipher.binary_encrypt(@data_str)
|
19
19
|
|
20
20
|
@data_encrypted_with_header = SymmetricEncryption::Cipher.build_header(
|
@@ -33,20 +33,20 @@ class ReaderTest < Minitest::Test
|
|
33
33
|
end
|
34
34
|
|
35
35
|
[true, false].each do |header|
|
36
|
-
|
37
|
-
|
36
|
+
describe header do
|
37
|
+
before do
|
38
38
|
@data_encrypted = header ? @data_encrypted_with_header : @data_encrypted_without_header
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
stream
|
41
|
+
it "#read()" do
|
42
|
+
stream = StringIO.new(@data_encrypted)
|
43
43
|
# Version 0 supplied if the file/stream does not have a header
|
44
|
-
decrypted = SymmetricEncryption::Reader.open(stream, version: 0) {|file| file.read}
|
44
|
+
decrypted = SymmetricEncryption::Reader.open(stream, version: 0) { |file| file.read }
|
45
45
|
assert_equal @data_str, decrypted
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
stream
|
48
|
+
it "#read(size) followed by #read()" do
|
49
|
+
stream = StringIO.new(@data_encrypted)
|
50
50
|
# Version 0 supplied if the file/stream does not have a header
|
51
51
|
decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
|
52
52
|
file.read(10)
|
@@ -55,9 +55,9 @@ class ReaderTest < Minitest::Test
|
|
55
55
|
assert_equal @data_str[10..-1], decrypted
|
56
56
|
end
|
57
57
|
|
58
|
-
|
59
|
-
stream
|
60
|
-
i
|
58
|
+
it "#each_line" do
|
59
|
+
stream = StringIO.new(@data_encrypted)
|
60
|
+
i = 0
|
61
61
|
# Version 0 supplied if the file/stream does not have a header
|
62
62
|
decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
|
63
63
|
file.each_line do |line|
|
@@ -67,13 +67,13 @@ class ReaderTest < Minitest::Test
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
|
71
|
-
stream
|
72
|
-
i
|
70
|
+
it "#read(size)" do
|
71
|
+
stream = StringIO.new(@data_encrypted)
|
72
|
+
i = 0
|
73
73
|
# Version 0 supplied if the file/stream does not have a header
|
74
74
|
decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
|
75
75
|
index = 0
|
76
|
-
[0,10,5,5000].each do |size|
|
76
|
+
[0, 10, 5, 5000].each do |size|
|
77
77
|
buf = file.read(size)
|
78
78
|
if size == 0
|
79
79
|
assert_equal '', buf
|
@@ -106,30 +106,30 @@ class ReaderTest < Minitest::Test
|
|
106
106
|
|
107
107
|
[:data, :empty, :blank].each do |usecase|
|
108
108
|
|
109
|
-
|
110
|
-
|
109
|
+
describe "read from #{usecase} file with options: #{options.inspect}" do
|
110
|
+
before do
|
111
111
|
case usecase
|
112
112
|
when :data
|
113
113
|
# Create encrypted file
|
114
|
-
@eof
|
114
|
+
@eof = false
|
115
115
|
@filename = '_test'
|
116
|
-
@header
|
116
|
+
@header = (options[:header] != false)
|
117
117
|
SymmetricEncryption::Writer.open(@filename, options) do |file|
|
118
|
-
@data.inject(0) {|sum,str| sum + file.write(str)}
|
118
|
+
@data.inject(0) { |sum, str| sum + file.write(str) }
|
119
119
|
end
|
120
120
|
when :empty
|
121
121
|
@data_str = ''
|
122
|
-
@eof
|
122
|
+
@eof = true
|
123
123
|
@filename = '_test_empty'
|
124
|
-
@header
|
124
|
+
@header = (options[:header] != false)
|
125
125
|
SymmetricEncryption::Writer.open(@filename, options) do |file|
|
126
126
|
# Leave data portion empty
|
127
127
|
end
|
128
128
|
when :blank
|
129
129
|
@data_str = ''
|
130
|
-
@eof
|
130
|
+
@eof = true
|
131
131
|
@filename = File.join(File.dirname(__FILE__), 'config/empty.csv')
|
132
|
-
@header
|
132
|
+
@header = false
|
133
133
|
assert_equal 0, File.size(@filename)
|
134
134
|
else
|
135
135
|
raise "Unhandled usecase: #{usecase}"
|
@@ -137,35 +137,35 @@ class ReaderTest < Minitest::Test
|
|
137
137
|
@data_size = @data_str.length
|
138
138
|
end
|
139
139
|
|
140
|
-
|
140
|
+
after do
|
141
141
|
File.delete(@filename) if File.exist?(@filename) && !@filename.end_with?('empty.csv')
|
142
142
|
end
|
143
143
|
|
144
|
-
|
144
|
+
it ".empty?" do
|
145
145
|
assert_equal (@data_size==0), SymmetricEncryption::Reader.empty?(@filename)
|
146
146
|
assert_raises Errno::ENOENT do
|
147
147
|
SymmetricEncryption::Reader.empty?('missing_file')
|
148
148
|
end
|
149
149
|
end
|
150
150
|
|
151
|
-
|
151
|
+
it ".header_present?" do
|
152
152
|
assert_equal @header, SymmetricEncryption::Reader.header_present?(@filename)
|
153
153
|
assert_raises Errno::ENOENT do
|
154
154
|
SymmetricEncryption::Reader.header_present?('missing_file')
|
155
155
|
end
|
156
156
|
end
|
157
157
|
|
158
|
-
|
158
|
+
it ".open return Zlib::GzipReader when compressed" do
|
159
159
|
file = SymmetricEncryption::Reader.open(@filename)
|
160
160
|
#assert_equal (@header && (options[:compress]||false)), file.is_a?(Zlib::GzipReader)
|
161
161
|
file.close
|
162
162
|
end
|
163
163
|
|
164
|
-
|
165
|
-
data
|
166
|
-
eof
|
164
|
+
it "#read()" do
|
165
|
+
data = nil
|
166
|
+
eof = nil
|
167
167
|
result = SymmetricEncryption::Reader.open(@filename) do |file|
|
168
|
-
eof
|
168
|
+
eof = file.eof?
|
169
169
|
data = file.read
|
170
170
|
end
|
171
171
|
assert_equal @eof, eof
|
@@ -173,9 +173,9 @@ class ReaderTest < Minitest::Test
|
|
173
173
|
assert_equal @data_str, result
|
174
174
|
end
|
175
175
|
|
176
|
-
|
176
|
+
it "#read(size)" do
|
177
177
|
file = SymmetricEncryption::Reader.open(@filename)
|
178
|
-
eof
|
178
|
+
eof = file.eof?
|
179
179
|
data = file.read(4096)
|
180
180
|
file.close
|
181
181
|
|
@@ -183,7 +183,7 @@ class ReaderTest < Minitest::Test
|
|
183
183
|
assert_equal (@data_size > 0 ? @data_str : nil), data
|
184
184
|
end
|
185
185
|
|
186
|
-
|
186
|
+
it "#each_line" do
|
187
187
|
decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
|
188
188
|
i = 0
|
189
189
|
file.each_line do |line|
|
@@ -193,7 +193,7 @@ class ReaderTest < Minitest::Test
|
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
|
-
|
196
|
+
it "#rewind" do
|
197
197
|
decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
|
198
198
|
file.read
|
199
199
|
file.rewind
|
@@ -202,10 +202,10 @@ class ReaderTest < Minitest::Test
|
|
202
202
|
assert_equal @data_str, decrypted
|
203
203
|
end
|
204
204
|
|
205
|
-
|
205
|
+
it "#gets(nil,size)" do
|
206
206
|
file = SymmetricEncryption::Reader.open(@filename)
|
207
|
-
eof
|
208
|
-
data = file.gets(nil,4096)
|
207
|
+
eof = file.eof?
|
208
|
+
data = file.gets(nil, 4096)
|
209
209
|
file.close
|
210
210
|
|
211
211
|
assert_equal @eof, eof
|
@@ -222,7 +222,7 @@ class ReaderTest < Minitest::Test
|
|
222
222
|
end
|
223
223
|
end
|
224
224
|
|
225
|
-
|
225
|
+
it "#gets(delim)" do
|
226
226
|
decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
|
227
227
|
i = 0
|
228
228
|
while line = file.gets("\n")
|
@@ -233,10 +233,10 @@ class ReaderTest < Minitest::Test
|
|
233
233
|
end
|
234
234
|
end
|
235
235
|
|
236
|
-
|
236
|
+
it "#gets(delim,size)" do
|
237
237
|
decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
|
238
238
|
i = 0
|
239
|
-
while line = file.gets("\n",128)
|
239
|
+
while line = file.gets("\n", 128)
|
240
240
|
i += 1
|
241
241
|
end
|
242
242
|
assert_equal (@data_size > 0 ? 3 : 0), i
|
@@ -247,25 +247,25 @@ class ReaderTest < Minitest::Test
|
|
247
247
|
end
|
248
248
|
end
|
249
249
|
|
250
|
-
|
251
|
-
|
250
|
+
describe "reading from files with previous keys" do
|
251
|
+
before do
|
252
252
|
@filename = '_test'
|
253
253
|
# Create encrypted file with old encryption key
|
254
254
|
SymmetricEncryption::Writer.open(@filename, version: 0) do |file|
|
255
|
-
@data.inject(0) {|sum,str| sum + file.write(str)}
|
255
|
+
@data.inject(0) { |sum, str| sum + file.write(str) }
|
256
256
|
end
|
257
257
|
end
|
258
258
|
|
259
|
-
|
259
|
+
after do
|
260
260
|
File.delete(@filename) if File.exist?(@filename)
|
261
261
|
end
|
262
262
|
|
263
|
-
|
264
|
-
decrypted = SymmetricEncryption::Reader.open(@filename) {|file| file.read}
|
263
|
+
it "decrypt from file in a single read" do
|
264
|
+
decrypted = SymmetricEncryption::Reader.open(@filename) { |file| file.read }
|
265
265
|
assert_equal @data_str, decrypted
|
266
266
|
end
|
267
267
|
|
268
|
-
|
268
|
+
it "decrypt from file a line at a time" do
|
269
269
|
decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
|
270
270
|
i = 0
|
271
271
|
file.each_line do |line|
|
@@ -275,7 +275,7 @@ class ReaderTest < Minitest::Test
|
|
275
275
|
end
|
276
276
|
end
|
277
277
|
|
278
|
-
|
278
|
+
it "support rewind" do
|
279
279
|
decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
|
280
280
|
file.read
|
281
281
|
file.rewind
|
@@ -285,16 +285,16 @@ class ReaderTest < Minitest::Test
|
|
285
285
|
end
|
286
286
|
end
|
287
287
|
|
288
|
-
|
289
|
-
|
288
|
+
describe "reading from files with previous keys without a header" do
|
289
|
+
before do
|
290
290
|
@filename = '_test'
|
291
291
|
# Create encrypted file with old encryption key
|
292
292
|
SymmetricEncryption::Writer.open(@filename, version: 0, header: false, random_key: false) do |file|
|
293
|
-
@data.inject(0) {|sum,str| sum + file.write(str)}
|
293
|
+
@data.inject(0) { |sum, str| sum + file.write(str) }
|
294
294
|
end
|
295
295
|
end
|
296
296
|
|
297
|
-
|
297
|
+
after do
|
298
298
|
begin
|
299
299
|
File.delete(@filename) if File.exist?(@filename)
|
300
300
|
rescue Errno::EACCES
|
@@ -302,15 +302,15 @@ class ReaderTest < Minitest::Test
|
|
302
302
|
end
|
303
303
|
end
|
304
304
|
|
305
|
-
|
306
|
-
decrypted = SymmetricEncryption::Reader.open(@filename, version: 0) {|file| file.read}
|
305
|
+
it "decrypt from file in a single read" do
|
306
|
+
decrypted = SymmetricEncryption::Reader.open(@filename, version: 0) { |file| file.read }
|
307
307
|
assert_equal @data_str, decrypted
|
308
308
|
end
|
309
309
|
|
310
|
-
|
310
|
+
it "decrypt from file in a single read with different version" do
|
311
311
|
# Should fail since file was encrypted using version 0 key
|
312
312
|
assert_raises OpenSSL::Cipher::CipherError do
|
313
|
-
SymmetricEncryption::Reader.open(@filename, version: 2) {|file| file.read}
|
313
|
+
SymmetricEncryption::Reader.open(@filename, version: 2) { |file| file.read }
|
314
314
|
end
|
315
315
|
end
|
316
316
|
end
|
@@ -3,15 +3,16 @@ require_relative 'test_helper'
|
|
3
3
|
# Unit Test for SymmetricEncryption
|
4
4
|
#
|
5
5
|
class SymmetricEncryptionTest < Minitest::Test
|
6
|
-
|
6
|
+
describe 'SymmetricEncryption' do
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
describe 'configuration' do
|
9
|
+
before do
|
10
|
+
config = SymmetricEncryption::Config.read_config(File.join(File.dirname(__FILE__), 'config', 'symmetric-encryption.yml'), 'test')
|
11
|
+
@ciphers = SymmetricEncryption::Config.extract_ciphers(config)
|
11
12
|
@cipher_v2, @cipher_v1, @cipher_v0 = @ciphers
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
+
it 'matches config file for first cipher' do
|
15
16
|
cipher = SymmetricEncryption.cipher
|
16
17
|
assert @cipher_v2.send(:key)
|
17
18
|
assert @cipher_v2.send(:iv)
|
@@ -21,7 +22,7 @@ class SymmetricEncryptionTest < Minitest::Test
|
|
21
22
|
assert_equal false, SymmetricEncryption.secondary_ciphers.include?(cipher)
|
22
23
|
end
|
23
24
|
|
24
|
-
|
25
|
+
it 'match config file for v1 cipher' do
|
25
26
|
cipher = SymmetricEncryption.cipher(2)
|
26
27
|
assert @cipher_v2.cipher_name
|
27
28
|
assert @cipher_v2.version
|
@@ -30,7 +31,7 @@ class SymmetricEncryptionTest < Minitest::Test
|
|
30
31
|
assert_equal false, SymmetricEncryption.secondary_ciphers.include?(cipher)
|
31
32
|
end
|
32
33
|
|
33
|
-
|
34
|
+
it 'match config file for v0 cipher' do
|
34
35
|
cipher = SymmetricEncryption.cipher(0)
|
35
36
|
assert @cipher_v0.cipher_name
|
36
37
|
assert @cipher_v0.version
|
@@ -41,43 +42,43 @@ class SymmetricEncryptionTest < Minitest::Test
|
|
41
42
|
end
|
42
43
|
|
43
44
|
SymmetricEncryption::Cipher::ENCODINGS.each do |encoding|
|
44
|
-
|
45
|
-
|
46
|
-
@social_security_number
|
47
|
-
@social_security_number_encrypted
|
45
|
+
describe "encoding: #{encoding}" do
|
46
|
+
before do
|
47
|
+
@social_security_number = '987654321'
|
48
|
+
@social_security_number_encrypted =
|
48
49
|
case encoding
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
50
|
+
when :base64
|
51
|
+
"QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA==\n"
|
52
|
+
when :base64strict
|
53
|
+
'QEVuQwIAS+8X1NRrqdfEIQyFHVPuVA=='
|
54
|
+
when :base16
|
55
|
+
'40456e4302004bef17d4d46ba9d7c4210c851d53ee54'
|
56
|
+
when :none
|
57
|
+
"@EnC\x02\x00K\xEF\x17\xD4\xD4k\xA9\xD7\xC4!\f\x85\x1DS\xEET".force_encoding(Encoding.find("binary"))
|
58
|
+
else
|
59
|
+
raise "Add test for encoding: #{encoding}"
|
60
|
+
end
|
60
61
|
@social_security_number_encrypted_with_secondary_1 = "D1UCu38pqJ3jc0GvwJHiow==\n"
|
61
|
-
@non_utf8
|
62
|
-
@encoding
|
63
|
-
SymmetricEncryption.cipher.encoding
|
62
|
+
@non_utf8 = "\xc2".force_encoding('binary')
|
63
|
+
@encoding = SymmetricEncryption.cipher.encoding
|
64
|
+
SymmetricEncryption.cipher.encoding = encoding
|
64
65
|
end
|
65
66
|
|
66
|
-
|
67
|
+
after do
|
67
68
|
SymmetricEncryption.cipher.encoding = @encoding
|
68
69
|
end
|
69
70
|
|
70
|
-
|
71
|
+
it "encrypt simple string" do
|
71
72
|
assert_equal @social_security_number_encrypted, SymmetricEncryption.encrypt(@social_security_number)
|
72
73
|
end
|
73
74
|
|
74
|
-
|
75
|
+
it "decrypt string" do
|
75
76
|
assert decrypted = SymmetricEncryption.decrypt(@social_security_number_encrypted)
|
76
77
|
assert_equal @social_security_number, decrypted
|
77
78
|
assert_equal Encoding.find('utf-8'), decrypted.encoding, decrypted
|
78
79
|
end
|
79
80
|
|
80
|
-
|
81
|
+
it 'return BINARY encoding for non-UTF-8 encrypted data' do
|
81
82
|
assert_equal Encoding.find('binary'), @non_utf8.encoding
|
82
83
|
assert_equal true, @non_utf8.valid_encoding?
|
83
84
|
assert encrypted = SymmetricEncryption.encrypt(@non_utf8)
|
@@ -87,34 +88,34 @@ class SymmetricEncryptionTest < Minitest::Test
|
|
87
88
|
assert_equal @non_utf8, decrypted
|
88
89
|
end
|
89
90
|
|
90
|
-
|
91
|
+
it "return nil when encrypting nil" do
|
91
92
|
assert_equal nil, SymmetricEncryption.encrypt(nil)
|
92
93
|
end
|
93
94
|
|
94
|
-
|
95
|
+
it "return '' when encrypting ''" do
|
95
96
|
assert_equal '', SymmetricEncryption.encrypt('')
|
96
97
|
end
|
97
98
|
|
98
|
-
|
99
|
+
it "return nil when decrypting nil" do
|
99
100
|
assert_equal nil, SymmetricEncryption.decrypt(nil)
|
100
101
|
end
|
101
102
|
|
102
|
-
|
103
|
+
it "return '' when decrypting ''" do
|
103
104
|
assert_equal '', SymmetricEncryption.decrypt('')
|
104
105
|
end
|
105
106
|
|
106
|
-
|
107
|
+
it "determine if string is encrypted" do
|
107
108
|
assert_equal true, SymmetricEncryption.encrypted?(@social_security_number_encrypted)
|
108
109
|
assert_equal false, SymmetricEncryption.encrypted?(@social_security_number)
|
109
110
|
end
|
110
111
|
end
|
111
112
|
end
|
112
113
|
|
113
|
-
|
114
|
-
|
114
|
+
describe "using select_cipher" do
|
115
|
+
before do
|
115
116
|
@social_security_number = "987654321"
|
116
117
|
# Encrypt data without a header and encode with base64 which has a trailing '\n'
|
117
|
-
@encrypted_0_ssn
|
118
|
+
@encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, false, false, false))
|
118
119
|
|
119
120
|
SymmetricEncryption.select_cipher do |encoded_str, decoded_str|
|
120
121
|
# Use cipher version 0 if the encoded string ends with "\n" otherwise
|
@@ -123,43 +124,43 @@ class SymmetricEncryptionTest < Minitest::Test
|
|
123
124
|
end
|
124
125
|
end
|
125
126
|
|
126
|
-
|
127
|
+
after do
|
127
128
|
# Clear out select_cipher
|
128
129
|
SymmetricEncryption.select_cipher
|
129
130
|
end
|
130
131
|
|
131
|
-
|
132
|
+
it "decrypt string without a header using an old cipher" do
|
132
133
|
assert_equal @social_security_number, SymmetricEncryption.decrypt(@encrypted_0_ssn)
|
133
134
|
end
|
134
135
|
end
|
135
136
|
|
136
|
-
|
137
|
-
|
137
|
+
describe "without select_cipher" do
|
138
|
+
before do
|
138
139
|
@social_security_number = "987654321"
|
139
140
|
# Encrypt data without a header and encode with base64 which has a trailing '\n'
|
140
|
-
assert @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number,false,false,false))
|
141
|
+
assert @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, false, false, false))
|
141
142
|
end
|
142
143
|
|
143
|
-
|
144
|
+
it "decrypt string without a header using an old cipher" do
|
144
145
|
assert_raises OpenSSL::Cipher::CipherError do
|
145
146
|
SymmetricEncryption.decrypt(@encrypted_0_ssn)
|
146
147
|
end
|
147
148
|
end
|
148
149
|
end
|
149
150
|
|
150
|
-
|
151
|
-
|
151
|
+
describe "random iv" do
|
152
|
+
before do
|
152
153
|
@social_security_number = "987654321"
|
153
154
|
end
|
154
155
|
|
155
|
-
|
156
|
+
it "encrypt and then decrypt using random iv" do
|
156
157
|
# Encrypt with random iv
|
157
158
|
assert encrypted = SymmetricEncryption.encrypt(@social_security_number, random_iv=true)
|
158
159
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
159
160
|
assert_equal @social_security_number, SymmetricEncryption.decrypt(encrypted)
|
160
161
|
end
|
161
162
|
|
162
|
-
|
163
|
+
it "encrypt and then decrypt using random iv with compression" do
|
163
164
|
# Encrypt with random iv and compress
|
164
165
|
assert encrypted = SymmetricEncryption.encrypt(@social_security_number, random_iv=true, compress=true)
|
165
166
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
@@ -167,122 +168,122 @@ class SymmetricEncryptionTest < Minitest::Test
|
|
167
168
|
end
|
168
169
|
end
|
169
170
|
|
170
|
-
|
171
|
-
|
172
|
-
|
171
|
+
describe "data types" do
|
172
|
+
describe "string" do
|
173
|
+
before do
|
173
174
|
@social_security_number = "987654321"
|
174
175
|
end
|
175
176
|
|
176
|
-
|
177
|
+
it "encrypt and decrypt value to and from a string" do
|
177
178
|
assert encrypted = SymmetricEncryption.encrypt(@social_security_number, random_iv=false, compress=false, type=:string)
|
178
179
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
179
180
|
assert_equal @social_security_number, SymmetricEncryption.decrypt(encrypted, version=nil, type=:string)
|
180
181
|
end
|
181
182
|
end
|
182
183
|
|
183
|
-
|
184
|
-
|
184
|
+
describe "integer" do
|
185
|
+
before do
|
185
186
|
@age = 21
|
186
187
|
end
|
187
188
|
|
188
|
-
|
189
|
+
it "encrypt and decrypt value to and from an integer" do
|
189
190
|
assert encrypted = SymmetricEncryption.encrypt(@age, random_iv=false, compress=false, type=:integer)
|
190
191
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
191
192
|
assert_equal @age, SymmetricEncryption.decrypt(encrypted, version=nil, type=:integer)
|
192
193
|
end
|
193
194
|
end
|
194
195
|
|
195
|
-
|
196
|
-
|
196
|
+
describe "float" do
|
197
|
+
before do
|
197
198
|
@miles = 2.5
|
198
199
|
end
|
199
200
|
|
200
|
-
|
201
|
+
it "encrypt and decrypt value to and from a float" do
|
201
202
|
assert encrypted = SymmetricEncryption.encrypt(@miles, random_iv=false, compress=false, type=:float)
|
202
203
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
203
204
|
assert_equal @miles, SymmetricEncryption.decrypt(encrypted, version=nil, type=:float)
|
204
205
|
end
|
205
206
|
end
|
206
207
|
|
207
|
-
|
208
|
-
|
208
|
+
describe "decimal" do
|
209
|
+
before do
|
209
210
|
@account_balance = BigDecimal.new("12.58")
|
210
211
|
end
|
211
212
|
|
212
|
-
|
213
|
+
it "encrypt and decrypt value to and from a BigDecimal" do
|
213
214
|
assert encrypted = SymmetricEncryption.encrypt(@account_balance, random_iv=false, compress=false, type=:decimal)
|
214
215
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
215
216
|
assert_equal @account_balance, SymmetricEncryption.decrypt(encrypted, version=nil, type=:decimal)
|
216
217
|
end
|
217
218
|
end
|
218
219
|
|
219
|
-
|
220
|
-
|
220
|
+
describe "datetime" do
|
221
|
+
before do
|
221
222
|
@checked_in_at = DateTime.new(2001, 11, 26, 20, 55, 54, "-5")
|
222
223
|
end
|
223
224
|
|
224
|
-
|
225
|
+
it "encrypt and decrypt value to and from a DateTime" do
|
225
226
|
assert encrypted = SymmetricEncryption.encrypt(@checked_in_at, random_iv=false, compress=false, type=:datetime)
|
226
227
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
227
228
|
assert_equal @checked_in_at, SymmetricEncryption.decrypt(encrypted, version=nil, type=:datetime)
|
228
229
|
end
|
229
230
|
end
|
230
231
|
|
231
|
-
|
232
|
-
|
232
|
+
describe "time" do
|
233
|
+
before do
|
233
234
|
@closing_time = Time.new(2013, 01, 01, 22, 30, 00, "-04:00")
|
234
235
|
end
|
235
236
|
|
236
|
-
|
237
|
+
it "encrypt and decrypt value to and from a Time" do
|
237
238
|
assert encrypted = SymmetricEncryption.encrypt(@closing_time, random_iv=false, compress=false, type=:time)
|
238
239
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
239
240
|
assert_equal @closing_time, SymmetricEncryption.decrypt(encrypted, version=nil, type=:time)
|
240
241
|
end
|
241
242
|
end
|
242
243
|
|
243
|
-
|
244
|
-
|
244
|
+
describe "date" do
|
245
|
+
before do
|
245
246
|
@birthdate = Date.new(1927, 04, 01)
|
246
247
|
end
|
247
248
|
|
248
|
-
|
249
|
+
it "encrypt and decrypt value to and from a Date" do
|
249
250
|
assert encrypted = SymmetricEncryption.encrypt(@birthdate, random_iv=false, compress=false, type=:date)
|
250
251
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
251
252
|
assert_equal @birthdate, SymmetricEncryption.decrypt(encrypted, version=nil, type=:date)
|
252
253
|
end
|
253
254
|
end
|
254
255
|
|
255
|
-
|
256
|
-
|
257
|
-
|
256
|
+
describe "boolean" do
|
257
|
+
describe "when true" do
|
258
|
+
before do
|
258
259
|
@is_working = true
|
259
260
|
end
|
260
261
|
|
261
|
-
|
262
|
+
it "encrypt and decrypt a true value to and from a boolean" do
|
262
263
|
assert encrypted = SymmetricEncryption.encrypt(@is_working, random_iv=false, compress=false, type=:boolean)
|
263
264
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
264
265
|
assert_equal @is_working, SymmetricEncryption.decrypt(encrypted, version=nil, type=:boolean)
|
265
266
|
end
|
266
267
|
end
|
267
268
|
|
268
|
-
|
269
|
-
|
269
|
+
describe "when false" do
|
270
|
+
before do
|
270
271
|
@is_broken = false
|
271
272
|
end
|
272
273
|
|
273
|
-
|
274
|
+
it "encrypt and decrypt a false value to and from a boolean" do
|
274
275
|
assert encrypted = SymmetricEncryption.encrypt(@is_broken, random_iv=false, compress=false, type=:boolean)
|
275
276
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
276
277
|
assert_equal @is_broken, SymmetricEncryption.decrypt(encrypted, version=nil, type=:boolean)
|
277
278
|
end
|
278
279
|
end
|
279
280
|
|
280
|
-
|
281
|
-
|
282
|
-
@test = {
|
281
|
+
describe "when yaml" do
|
282
|
+
before do
|
283
|
+
@test = {:a => :b}
|
283
284
|
end
|
284
285
|
|
285
|
-
|
286
|
+
it "encrypt and decrypt a false value to and from a boolean" do
|
286
287
|
assert encrypted = SymmetricEncryption.encrypt(@test, random_iv=false, compress=false, type=:yaml)
|
287
288
|
assert_equal true, SymmetricEncryption.encrypted?(encrypted)
|
288
289
|
assert_equal @test, SymmetricEncryption.decrypt(encrypted, version=nil, type=:yaml)
|