symmetric-encryption 3.8.2 → 3.8.3

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.
@@ -9,7 +9,7 @@ class ReaderTest < Minitest::Test
9
9
  @data = [
10
10
  "Hello World\n",
11
11
  "Keep this secret\n",
12
- "And keep going even further and further..."
12
+ 'And keep going even further and further...'
13
13
  ]
14
14
  @data_str = @data.inject('') { |sum, str| sum << str }
15
15
  @data_len = @data_str.length
@@ -19,7 +19,7 @@ class ReaderTest < Minitest::Test
19
19
 
20
20
  @data_encrypted_with_header = SymmetricEncryption::Cipher.build_header(
21
21
  @cipher.version,
22
- compress = false,
22
+ false,
23
23
  @cipher.send(:iv),
24
24
  @cipher.send(:key),
25
25
  @cipher.cipher_name
@@ -59,7 +59,7 @@ class ReaderTest < Minitest::Test
59
59
  stream = StringIO.new(@data_encrypted)
60
60
  i = 0
61
61
  # Version 0 supplied if the file/stream does not have a header
62
- decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
62
+ SymmetricEncryption::Reader.open(stream, version: 0) do |file|
63
63
  file.each_line do |line|
64
64
  assert_equal @data[i], line
65
65
  i += 1
@@ -69,9 +69,8 @@ class ReaderTest < Minitest::Test
69
69
 
70
70
  it "#read(size)" do
71
71
  stream = StringIO.new(@data_encrypted)
72
- i = 0
73
72
  # Version 0 supplied if the file/stream does not have a header
74
- decrypted = SymmetricEncryption::Reader.open(stream, version: 0) do |file|
73
+ SymmetricEncryption::Reader.open(stream, version: 0) do |file|
75
74
  index = 0
76
75
  [0, 10, 5, 5000].each do |size|
77
76
  buf = file.read(size)
@@ -155,7 +154,7 @@ class ReaderTest < Minitest::Test
155
154
  end
156
155
  end
157
156
 
158
- it ".open return Zlib::GzipReader when compressed" do
157
+ it '.open return Zlib::GzipReader when compressed' do
159
158
  file = SymmetricEncryption::Reader.open(@filename)
160
159
  #assert_equal (@header && (options[:compress]||false)), file.is_a?(Zlib::GzipReader)
161
160
  file.close
@@ -184,7 +183,7 @@ class ReaderTest < Minitest::Test
184
183
  end
185
184
 
186
185
  it "#each_line" do
187
- decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
186
+ SymmetricEncryption::Reader.open(@filename) do |file|
188
187
  i = 0
189
188
  file.each_line do |line|
190
189
  assert_equal @data[i], line
@@ -223,7 +222,7 @@ class ReaderTest < Minitest::Test
223
222
  end
224
223
 
225
224
  it "#gets(delim)" do
226
- decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
225
+ SymmetricEncryption::Reader.open(@filename) do |file|
227
226
  i = 0
228
227
  while line = file.gets("\n")
229
228
  assert_equal @data[i], line
@@ -234,9 +233,9 @@ class ReaderTest < Minitest::Test
234
233
  end
235
234
 
236
235
  it "#gets(delim,size)" do
237
- decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
236
+ SymmetricEncryption::Reader.open(@filename) do |file|
238
237
  i = 0
239
- while line = file.gets("\n", 128)
238
+ while file.gets("\n", 128)
240
239
  i += 1
241
240
  end
242
241
  assert_equal (@data_size > 0 ? 3 : 0), i
@@ -247,7 +246,7 @@ class ReaderTest < Minitest::Test
247
246
  end
248
247
  end
249
248
 
250
- describe "reading from files with previous keys" do
249
+ describe 'reading from files with previous keys' do
251
250
  before do
252
251
  @filename = '_test'
253
252
  # Create encrypted file with old encryption key
@@ -260,13 +259,13 @@ class ReaderTest < Minitest::Test
260
259
  File.delete(@filename) if File.exist?(@filename)
261
260
  end
262
261
 
263
- it "decrypt from file in a single read" do
262
+ it 'decrypt from file in a single read' do
264
263
  decrypted = SymmetricEncryption::Reader.open(@filename) { |file| file.read }
265
264
  assert_equal @data_str, decrypted
266
265
  end
267
266
 
268
- it "decrypt from file a line at a time" do
269
- decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
267
+ it 'decrypt from file a line at a time' do
268
+ SymmetricEncryption::Reader.open(@filename) do |file|
270
269
  i = 0
271
270
  file.each_line do |line|
272
271
  assert_equal @data[i], line
@@ -275,7 +274,7 @@ class ReaderTest < Minitest::Test
275
274
  end
276
275
  end
277
276
 
278
- it "support rewind" do
277
+ it 'support rewind' do
279
278
  decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
280
279
  file.read
281
280
  file.rewind
@@ -285,7 +284,7 @@ class ReaderTest < Minitest::Test
285
284
  end
286
285
  end
287
286
 
288
- describe "reading from files with previous keys without a header" do
287
+ describe 'reading from files with previous keys without a header' do
289
288
  before do
290
289
  @filename = '_test'
291
290
  # Create encrypted file with old encryption key
@@ -302,12 +301,12 @@ class ReaderTest < Minitest::Test
302
301
  end
303
302
  end
304
303
 
305
- it "decrypt from file in a single read" do
304
+ it 'decrypt from file in a single read' do
306
305
  decrypted = SymmetricEncryption::Reader.open(@filename, version: 0) { |file| file.read }
307
306
  assert_equal @data_str, decrypted
308
307
  end
309
308
 
310
- it "decrypt from file in a single read with different version" do
309
+ it 'decrypt from file in a single read with different version' do
311
310
  # Should fail since file was encrypted using version 0 key
312
311
  assert_raises OpenSSL::Cipher::CipherError do
313
312
  SymmetricEncryption::Reader.open(@filename, version: 2) { |file| file.read }
@@ -54,7 +54,7 @@ class SymmetricEncryptionTest < Minitest::Test
54
54
  when :base16
55
55
  '40456e4302004bef17d4d46ba9d7c4210c851d53ee54'
56
56
  when :none
57
- "@EnC\x02\x00K\xEF\x17\xD4\xD4k\xA9\xD7\xC4!\f\x85\x1DS\xEET".force_encoding(Encoding.find("binary"))
57
+ "@EnC\x02\x00K\xEF\x17\xD4\xD4k\xA9\xD7\xC4!\f\x85\x1DS\xEET".force_encoding(Encoding.find('binary'))
58
58
  else
59
59
  raise "Add test for encoding: #{encoding}"
60
60
  end
@@ -68,11 +68,11 @@ class SymmetricEncryptionTest < Minitest::Test
68
68
  SymmetricEncryption.cipher.encoding = @encoding
69
69
  end
70
70
 
71
- it "encrypt simple string" do
71
+ it 'encrypt simple string' do
72
72
  assert_equal @social_security_number_encrypted, SymmetricEncryption.encrypt(@social_security_number)
73
73
  end
74
74
 
75
- it "decrypt string" do
75
+ it 'decrypt string' do
76
76
  assert decrypted = SymmetricEncryption.decrypt(@social_security_number_encrypted)
77
77
  assert_equal @social_security_number, decrypted
78
78
  assert_equal Encoding.find('utf-8'), decrypted.encoding, decrypted
@@ -88,7 +88,7 @@ class SymmetricEncryptionTest < Minitest::Test
88
88
  assert_equal @non_utf8, decrypted
89
89
  end
90
90
 
91
- it "return nil when encrypting nil" do
91
+ it 'return nil when encrypting nil' do
92
92
  assert_equal nil, SymmetricEncryption.encrypt(nil)
93
93
  end
94
94
 
@@ -96,7 +96,7 @@ class SymmetricEncryptionTest < Minitest::Test
96
96
  assert_equal '', SymmetricEncryption.encrypt('')
97
97
  end
98
98
 
99
- it "return nil when decrypting nil" do
99
+ it 'return nil when decrypting nil' do
100
100
  assert_equal nil, SymmetricEncryption.decrypt(nil)
101
101
  end
102
102
 
@@ -104,16 +104,16 @@ class SymmetricEncryptionTest < Minitest::Test
104
104
  assert_equal '', SymmetricEncryption.decrypt('')
105
105
  end
106
106
 
107
- it "determine if string is encrypted" do
107
+ it 'determine if string is encrypted' do
108
108
  assert_equal true, SymmetricEncryption.encrypted?(@social_security_number_encrypted)
109
109
  assert_equal false, SymmetricEncryption.encrypted?(@social_security_number)
110
110
  end
111
111
  end
112
112
  end
113
113
 
114
- describe "using select_cipher" do
114
+ describe 'using select_cipher' do
115
115
  before do
116
- @social_security_number = "987654321"
116
+ @social_security_number = '987654321'
117
117
  # Encrypt data without a header and encode with base64 which has a trailing '\n'
118
118
  @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, false, false, false))
119
119
 
@@ -129,164 +129,164 @@ class SymmetricEncryptionTest < Minitest::Test
129
129
  SymmetricEncryption.select_cipher
130
130
  end
131
131
 
132
- it "decrypt string without a header using an old cipher" do
132
+ it 'decrypt string without a header using an old cipher' do
133
133
  assert_equal @social_security_number, SymmetricEncryption.decrypt(@encrypted_0_ssn)
134
134
  end
135
135
  end
136
136
 
137
- describe "without select_cipher" do
137
+ describe 'without select_cipher' do
138
138
  before do
139
- @social_security_number = "987654321"
139
+ @social_security_number = '987654321'
140
140
  # Encrypt data without a header and encode with base64 which has a trailing '\n'
141
141
  assert @encrypted_0_ssn = SymmetricEncryption.cipher(0).encode(SymmetricEncryption.cipher(0).binary_encrypt(@social_security_number, false, false, false))
142
142
  end
143
143
 
144
- it "decrypt string without a header using an old cipher" do
144
+ it 'decrypt string without a header using an old cipher' do
145
145
  assert_raises OpenSSL::Cipher::CipherError do
146
146
  SymmetricEncryption.decrypt(@encrypted_0_ssn)
147
147
  end
148
148
  end
149
149
  end
150
150
 
151
- describe "random iv" do
151
+ describe 'random iv' do
152
152
  before do
153
- @social_security_number = "987654321"
153
+ @social_security_number = '987654321'
154
154
  end
155
155
 
156
- it "encrypt and then decrypt using random iv" do
156
+ it 'encrypt and then decrypt using random iv' do
157
157
  # Encrypt with random iv
158
- assert encrypted = SymmetricEncryption.encrypt(@social_security_number, random_iv=true)
158
+ assert encrypted = SymmetricEncryption.encrypt(@social_security_number, true)
159
159
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
160
160
  assert_equal @social_security_number, SymmetricEncryption.decrypt(encrypted)
161
161
  end
162
162
 
163
- it "encrypt and then decrypt using random iv with compression" do
163
+ it 'encrypt and then decrypt using random iv with compression' do
164
164
  # Encrypt with random iv and compress
165
- assert encrypted = SymmetricEncryption.encrypt(@social_security_number, random_iv=true, compress=true)
165
+ assert encrypted = SymmetricEncryption.encrypt(@social_security_number, true, true)
166
166
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
167
167
  assert_equal @social_security_number, SymmetricEncryption.decrypt(encrypted)
168
168
  end
169
169
  end
170
170
 
171
- describe "data types" do
172
- describe "string" do
171
+ describe 'data types' do
172
+ describe 'string' do
173
173
  before do
174
- @social_security_number = "987654321"
174
+ @social_security_number = '987654321'
175
175
  end
176
176
 
177
- it "encrypt and decrypt value to and from a string" do
178
- assert encrypted = SymmetricEncryption.encrypt(@social_security_number, random_iv=false, compress=false, type=:string)
177
+ it 'encrypt and decrypt value to and from a string' do
178
+ assert encrypted = SymmetricEncryption.encrypt(@social_security_number, false, false, :string)
179
179
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
180
- assert_equal @social_security_number, SymmetricEncryption.decrypt(encrypted, version=nil, type=:string)
180
+ assert_equal @social_security_number, SymmetricEncryption.decrypt(encrypted, nil, :string)
181
181
  end
182
182
  end
183
183
 
184
- describe "integer" do
184
+ describe 'integer' do
185
185
  before do
186
186
  @age = 21
187
187
  end
188
188
 
189
- it "encrypt and decrypt value to and from an integer" do
190
- assert encrypted = SymmetricEncryption.encrypt(@age, random_iv=false, compress=false, type=:integer)
189
+ it 'encrypt and decrypt value to and from an integer' do
190
+ assert encrypted = SymmetricEncryption.encrypt(@age, false, false, :integer)
191
191
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
192
- assert_equal @age, SymmetricEncryption.decrypt(encrypted, version=nil, type=:integer)
192
+ assert_equal @age, SymmetricEncryption.decrypt(encrypted, nil, :integer)
193
193
  end
194
194
  end
195
195
 
196
- describe "float" do
196
+ describe 'float' do
197
197
  before do
198
198
  @miles = 2.5
199
199
  end
200
200
 
201
- it "encrypt and decrypt value to and from a float" do
202
- assert encrypted = SymmetricEncryption.encrypt(@miles, random_iv=false, compress=false, type=:float)
201
+ it 'encrypt and decrypt value to and from a float' do
202
+ assert encrypted = SymmetricEncryption.encrypt(@miles, false, false, :float)
203
203
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
204
- assert_equal @miles, SymmetricEncryption.decrypt(encrypted, version=nil, type=:float)
204
+ assert_equal @miles, SymmetricEncryption.decrypt(encrypted, nil, :float)
205
205
  end
206
206
  end
207
207
 
208
- describe "decimal" do
208
+ describe 'decimal' do
209
209
  before do
210
- @account_balance = BigDecimal.new("12.58")
210
+ @account_balance = BigDecimal.new('12.58')
211
211
  end
212
212
 
213
- it "encrypt and decrypt value to and from a BigDecimal" do
214
- assert encrypted = SymmetricEncryption.encrypt(@account_balance, random_iv=false, compress=false, type=:decimal)
213
+ it 'encrypt and decrypt value to and from a BigDecimal' do
214
+ assert encrypted = SymmetricEncryption.encrypt(@account_balance, false, false, :decimal)
215
215
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
216
- assert_equal @account_balance, SymmetricEncryption.decrypt(encrypted, version=nil, type=:decimal)
216
+ assert_equal @account_balance, SymmetricEncryption.decrypt(encrypted, nil, :decimal)
217
217
  end
218
218
  end
219
219
 
220
- describe "datetime" do
220
+ describe 'datetime' do
221
221
  before do
222
222
  @checked_in_at = DateTime.new(2001, 11, 26, 20, 55, 54, "-5")
223
223
  end
224
224
 
225
- it "encrypt and decrypt value to and from a DateTime" do
226
- assert encrypted = SymmetricEncryption.encrypt(@checked_in_at, random_iv=false, compress=false, type=:datetime)
225
+ it 'encrypt and decrypt value to and from a DateTime' do
226
+ assert encrypted = SymmetricEncryption.encrypt(@checked_in_at, false, false, :datetime)
227
227
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
228
- assert_equal @checked_in_at, SymmetricEncryption.decrypt(encrypted, version=nil, type=:datetime)
228
+ assert_equal @checked_in_at, SymmetricEncryption.decrypt(encrypted, nil, :datetime)
229
229
  end
230
230
  end
231
231
 
232
- describe "time" do
232
+ describe 'time' do
233
233
  before do
234
234
  @closing_time = Time.new(2013, 01, 01, 22, 30, 00, "-04:00")
235
235
  end
236
236
 
237
- it "encrypt and decrypt value to and from a Time" do
238
- assert encrypted = SymmetricEncryption.encrypt(@closing_time, random_iv=false, compress=false, type=:time)
237
+ it 'encrypt and decrypt value to and from a Time' do
238
+ assert encrypted = SymmetricEncryption.encrypt(@closing_time, false, false, :time)
239
239
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
240
- assert_equal @closing_time, SymmetricEncryption.decrypt(encrypted, version=nil, type=:time)
240
+ assert_equal @closing_time, SymmetricEncryption.decrypt(encrypted, nil, :time)
241
241
  end
242
242
  end
243
243
 
244
- describe "date" do
244
+ describe 'date' do
245
245
  before do
246
246
  @birthdate = Date.new(1927, 04, 01)
247
247
  end
248
248
 
249
- it "encrypt and decrypt value to and from a Date" do
250
- assert encrypted = SymmetricEncryption.encrypt(@birthdate, random_iv=false, compress=false, type=:date)
249
+ it 'encrypt and decrypt value to and from a Date' do
250
+ assert encrypted = SymmetricEncryption.encrypt(@birthdate, false, false, :date)
251
251
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
252
- assert_equal @birthdate, SymmetricEncryption.decrypt(encrypted, version=nil, type=:date)
252
+ assert_equal @birthdate, SymmetricEncryption.decrypt(encrypted, nil, :date)
253
253
  end
254
254
  end
255
255
 
256
- describe "boolean" do
257
- describe "when true" do
256
+ describe 'boolean' do
257
+ describe 'when true' do
258
258
  before do
259
259
  @is_working = true
260
260
  end
261
261
 
262
- it "encrypt and decrypt a true value to and from a boolean" do
263
- assert encrypted = SymmetricEncryption.encrypt(@is_working, random_iv=false, compress=false, type=:boolean)
262
+ it 'encrypt and decrypt a true value to and from a boolean' do
263
+ assert encrypted = SymmetricEncryption.encrypt(@is_working, false, false, :boolean)
264
264
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
265
- assert_equal @is_working, SymmetricEncryption.decrypt(encrypted, version=nil, type=:boolean)
265
+ assert_equal @is_working, SymmetricEncryption.decrypt(encrypted, nil, :boolean)
266
266
  end
267
267
  end
268
268
 
269
- describe "when false" do
269
+ describe 'when false' do
270
270
  before do
271
271
  @is_broken = false
272
272
  end
273
273
 
274
- it "encrypt and decrypt a false value to and from a boolean" do
275
- assert encrypted = SymmetricEncryption.encrypt(@is_broken, random_iv=false, compress=false, type=:boolean)
274
+ it 'encrypt and decrypt a false value to and from a boolean' do
275
+ assert encrypted = SymmetricEncryption.encrypt(@is_broken, false, false, :boolean)
276
276
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
277
- assert_equal @is_broken, SymmetricEncryption.decrypt(encrypted, version=nil, type=:boolean)
277
+ assert_equal @is_broken, SymmetricEncryption.decrypt(encrypted, nil, :boolean)
278
278
  end
279
279
  end
280
280
 
281
- describe "when yaml" do
281
+ describe 'when yaml' do
282
282
  before do
283
283
  @test = {:a => :b}
284
284
  end
285
285
 
286
- it "encrypt and decrypt a false value to and from a boolean" do
287
- assert encrypted = SymmetricEncryption.encrypt(@test, random_iv=false, compress=false, type=:yaml)
286
+ it 'encrypt and decrypt a false value to and from a boolean' do
287
+ assert encrypted = SymmetricEncryption.encrypt(@test, false, false, :yaml)
288
288
  assert_equal true, SymmetricEncryption.encrypted?(encrypted)
289
- assert_equal @test, SymmetricEncryption.decrypt(encrypted, version=nil, type=:yaml)
289
+ assert_equal @test, SymmetricEncryption.decrypt(encrypted, nil, :yaml)
290
290
  end
291
291
  end
292
292
 
Binary file
@@ -10,9 +10,15 @@ require 'active_record'
10
10
  require 'symmetric-encryption'
11
11
  require 'awesome_print'
12
12
 
13
- Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
13
+ begin
14
+ require 'active_model/serializers'
15
+ rescue LoadError
16
+ # Only used when running Rails 5 and MongoMapper
17
+ end
14
18
 
15
- SemanticLogger.add_appender('test.log', &SemanticLogger::Appender::Base.colorized_formatter)
19
+ #Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
20
+
21
+ SemanticLogger.add_appender(file_name: 'test.log', formatter: :color)
16
22
  SemanticLogger.default_level = :trace
17
23
 
18
24
  # Load Symmetric Encryption keys
@@ -9,7 +9,7 @@ class WriterTest < Minitest::Test
9
9
  @data = [
10
10
  "Hello World\n",
11
11
  "Keep this secret\n",
12
- "And keep going even further and further..."
12
+ 'And keep going even further and further...'
13
13
  ]
14
14
  @data_str = @data.inject('') { |sum, str| sum << str }
15
15
  @data_len = @data_str.length
@@ -25,7 +25,7 @@ class WriterTest < Minitest::Test
25
25
  File.delete(@filename) if File.exist?(@filename)
26
26
  end
27
27
 
28
- it "encrypt to string stream" do
28
+ it 'encrypt to string stream' do
29
29
  stream = StringIO.new
30
30
  file = SymmetricEncryption::Writer.new(stream, header: false, random_key: false, random_iv: false)
31
31
  written_len = @data.inject(0) { |sum, str| sum + file.write(str) }
@@ -38,7 +38,7 @@ class WriterTest < Minitest::Test
38
38
  assert_equal @data_encrypted, result
39
39
  end
40
40
 
41
- it "encrypt to string stream using .open" do
41
+ it 'encrypt to string stream using .open' do
42
42
  written_len = 0
43
43
  stream = StringIO.new
44
44
  SymmetricEncryption::Writer.open(stream) do |file|
@@ -48,7 +48,7 @@ class WriterTest < Minitest::Test
48
48
  assert_equal @data_len, written_len
49
49
  end
50
50
 
51
- it "encrypt to file using .open" do
51
+ it 'encrypt to file using .open' do
52
52
  written_len = nil
53
53
  SymmetricEncryption::Writer.open(@filename, header: false, random_key: false, random_iv: false) do |file|
54
54
  written_len = @data.inject(0) { |sum, str| sum + file.write(str) }