symmetric-encryption 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,9 +6,9 @@
6
6
  # can be placed directly in the source code.
7
7
  # And therefore no RSA private key is required
8
8
  development: &development_defaults
9
- key: 1234567890ABCDEF1234567890ABCDEF
10
- iv: 1234567890ABCDEF
11
- cipher: aes-128-cbc
9
+ key: 1234567890ABCDEF1234567890ABCDEF
10
+ iv: 1234567890ABCDEF
11
+ cipher: aes-128-cbc
12
12
 
13
13
  test:
14
14
  <<: *development_defaults
@@ -0,0 +1 @@
1
+ "������ W��ga�
@@ -67,8 +67,7 @@ module SymmetricEncryption
67
67
  #
68
68
  # require 'fastercsv'
69
69
  # begin
70
- # # Must supply :row_sep for FasterCSV otherwise it will attempt to rewind the file etc.
71
- # csv = FasterCSV.new(SymmetricEncryption::Reader.open('csv_encrypted'), :row_sep => "\n")
70
+ # csv = FasterCSV.new(SymmetricEncryption::Reader.open('csv_encrypted'))
72
71
  # csv.each {|row| p row}
73
72
  # ensure
74
73
  # csv.close if csv
@@ -92,29 +91,8 @@ module SymmetricEncryption
92
91
  def initialize(ios,options={})
93
92
  @ios = ios
94
93
  @buffer_size = options.fetch(:buffer_size, 4096).to_i
95
- @compressed = nil
96
- @read_buffer = ''
97
-
98
- # Read first block and check for the header
99
- buf = @ios.read(@buffer_size)
100
- if buf.start_with?(SymmetricEncryption::MAGIC_HEADER)
101
- # Header includes magic header and version byte
102
- # Remove header and extract flags
103
- header, flags = buf.slice!(0..MAGIC_HEADER_SIZE+1).unpack(MAGIC_HEADER_UNPACK)
104
- @compressed = (flags & 0b1000_0000_0000_0000) != 0
105
- @version = @compressed ? flags - 0b1000_0000_0000_0000 : flags
106
- else
107
- @version = options[:version]
108
- end
109
-
110
- # Use primary cipher by default, but allow a secondary cipher to be selected for encryption
111
- @cipher = SymmetricEncryption.cipher(@version)
112
- raise "Cipher with version:#{@version} not found in any of the configured SymmetricEncryption ciphers" unless @cipher
113
- @stream_cipher = @cipher.send(:openssl_cipher, :decrypt)
114
-
115
- # First call to #update should return an empty string anyway
116
- @read_buffer << @stream_cipher.update(buf)
117
- @read_buffer << @stream_cipher.final if @ios.eof?
94
+ @version = options[:version]
95
+ read_header
118
96
  end
119
97
 
120
98
  # Returns whether the stream being read is compressed
@@ -186,6 +164,7 @@ module SymmetricEncryption
186
164
  data << @stream_cipher.final
187
165
  end
188
166
  end
167
+ @pos += data.length
189
168
  data
190
169
  end
191
170
 
@@ -209,6 +188,7 @@ module SymmetricEncryption
209
188
  end
210
189
  index ||= -1
211
190
  data = @read_buffer.slice!(0..index)
191
+ @pos += data.length
212
192
  return nil if data.length == 0 && eof?
213
193
  data
214
194
  end
@@ -231,21 +211,45 @@ module SymmetricEncryption
231
211
  (@read_buffer.size == 0) && @ios.eof?
232
212
  end
233
213
 
234
- # Return the approximate offset in bytes of the current input stream
235
- # Since the encrypted data size does not match the unencrypted size
236
- # this value cannot be guaranteed. Especially if compression is turned on
214
+ # Return the number of bytes read so far from the input stream
237
215
  def pos
238
- @ios.pos - @read_buffer.size
216
+ @pos
239
217
  end
240
218
 
241
219
  # Rewind back to the beginning of the file
242
220
  def rewind
243
221
  @read_buffer = ''
244
222
  @ios.rewind
223
+ read_header
245
224
  end
246
225
 
247
226
  private
248
227
 
228
+ # Read the header from the file if present
229
+ def read_header
230
+ @compressed = nil
231
+ @pos = 0
232
+
233
+ # Read first block and check for the header
234
+ buf = @ios.read(@buffer_size)
235
+ if buf.start_with?(MAGIC_HEADER)
236
+ # Header includes magic header and version byte
237
+ # Remove header and extract flags
238
+ header, flags = buf.slice!(0..MAGIC_HEADER_SIZE+1).unpack(MAGIC_HEADER_UNPACK)
239
+ @compressed = (flags & 0b1000_0000_0000_0000) != 0
240
+ @version = @compressed ? flags - 0b1000_0000_0000_0000 : flags
241
+ end
242
+
243
+ # Use primary cipher by default, but allow a secondary cipher to be selected for encryption
244
+ @cipher = SymmetricEncryption.cipher(@version)
245
+ raise "Cipher with version:#{@version.inspect} not found in any of the configured SymmetricEncryption ciphers" unless @cipher
246
+ @stream_cipher = @cipher.send(:openssl_cipher, :decrypt)
247
+
248
+ # First call to #update should return an empty string anyway
249
+ @read_buffer = @stream_cipher.update(buf)
250
+ @read_buffer << @stream_cipher.final if @ios.eof?
251
+ end
252
+
249
253
  # Read a block of data and append the decrypted data in the read buffer
250
254
  def read_block
251
255
  buf = @ios.read(@buffer_size)
@@ -13,6 +13,14 @@ module SymmetricEncryption
13
13
  @@secondary_ciphers = []
14
14
 
15
15
  # Set the Primary Symmetric Cipher to be used
16
+ #
17
+ # Example: For testing purposes the following test cipher can be used:
18
+ #
19
+ # SymmetricEncryption.cipher = SymmetricEncryption::Cipher.new(
20
+ # :key => '1234567890ABCDEF1234567890ABCDEF',
21
+ # :iv => '1234567890ABCDEF',
22
+ # :cipher => 'aes-128-cbc'
23
+ # )
16
24
  def self.cipher=(cipher)
17
25
  raise "Cipher must be similar to SymmetricEncryption::Ciphers" unless cipher.respond_to?(:encrypt) && cipher.respond_to?(:decrypt)
18
26
  @@cipher = cipher
@@ -22,6 +30,7 @@ module SymmetricEncryption
22
30
  # If a version is supplied, then the cipher matching that version will be
23
31
  # returned or nil if no match was found
24
32
  def self.cipher(version = 0)
33
+ raise "Call SymmetricEncryption.load! or SymmetricEncryption.cipher= prior to encrypting or decrypting data" unless @@cipher
25
34
  return @@cipher if version.nil? || (version == 0) || (@@cipher.version == version)
26
35
  secondary_ciphers.find {|c| c.version == version}
27
36
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module SymmetricEncryption #:nodoc
3
- VERSION = "0.6.0"
3
+ VERSION = "0.6.1"
4
4
  end
@@ -98,6 +98,16 @@ class ReaderTest < Test::Unit::TestCase
98
98
  end
99
99
  end
100
100
  end
101
+
102
+ should "support rewind" do
103
+ decrypted = SymmetricEncryption::Reader.open(@filename) do |file|
104
+ file.read
105
+ file.rewind
106
+ file.read
107
+ end
108
+ assert_equal @data_str, decrypted
109
+ end
110
+
101
111
  end
102
112
  end
103
113
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 0
9
- version: 0.6.0
8
+ - 1
9
+ version: 0.6.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Reid Morrison
@@ -29,6 +29,7 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - examples/symmetric-encryption.yml
32
+ - lib/csv_encrypted
32
33
  - lib/symmetric-encryption.rb
33
34
  - lib/symmetric_encryption/cipher.rb
34
35
  - lib/symmetric_encryption/extensions/active_record/base.rb
@@ -49,6 +50,8 @@ files:
49
50
  - nbproject/project.xml
50
51
  - Rakefile
51
52
  - README.md
53
+ - symmetric-encryption-0.6.0.gem
54
+ - symmetric-encryption-0.6.1.gem
52
55
  - test/attr_encrypted_test.rb
53
56
  - test/cipher_test.rb
54
57
  - test/config/database.yml