lib/symmetric_encryption/reader.rb in symmetric-encryption-3.7.2 vs lib/symmetric_encryption/reader.rb in symmetric-encryption-3.8.0

- old
+ new

@@ -92,26 +92,26 @@ end # Returns [true|false] whether the file or stream contains any data # excluding the header should it have one def self.empty?(filename_or_stream) - open(filename_or_stream) {|file| file.eof? } + open(filename_or_stream) { |file| file.eof? } end # Returns [true|false] whether the file contains the encryption header def self.header_present?(filename) - ::File.open(filename, 'rb') {|file| new(file).header_present?} + ::File.open(filename, 'rb') { |file| new(file).header_present? } end # After opening a file Returns [true|false] whether the file being # read has an encryption header def header_present? @header_present end # Decrypt data before reading from the supplied stream - def initialize(ios,options={}) + def initialize(ios, options={}) @ios = ios @buffer_size = options.fetch(:buffer_size, 4096).to_i @version = options[:version] @header_present = false @closed = false @@ -191,16 +191,16 @@ if @read_buffer.length == 0 data = nil elsif @read_buffer.length > length data = @read_buffer.slice!(0..length-1) else - data = @read_buffer + data = @read_buffer @read_buffer = '' end else # Capture anything already in the buffer - data = @read_buffer + data = @read_buffer @read_buffer = '' if !@ios.eof? # Read entire file buf = @ios.read || '' @@ -214,28 +214,28 @@ # Reads a single decrypted line from the file up to and including the optional sep_string. # Raises EOFError on eof # The stream must be opened for reading or an IOError will be raised. def readline(sep_string = "\n") - gets(sep_string) || raise(EOFError.new("End of file reached when trying to read a line")) + gets(sep_string) || raise(EOFError.new('End of file reached when trying to read a line')) end # Reads a single decrypted line from the file up to and including the optional sep_string. # A sep_string of nil reads the entire contents of the file # Returns nil on eof # The stream must be opened for reading or an IOError will be raised. - def gets(sep_string,length=nil) + def gets(sep_string, length=nil) return read(length) if sep_string.nil? # Read more data until we get the sep_string while (index = @read_buffer.index(sep_string)).nil? && !@ios.eof? break if length && @read_buffer.length >= length read_block end index ||= -1 - data = @read_buffer.slice!(0..index) - @pos += data.length + data = @read_buffer.slice!(0..index) + @pos += data.length return nil if data.length == 0 && eof? data end # ios.each(sep_string="\n") {|line| block } => ios @@ -298,11 +298,11 @@ # Read and decrypt entire file a block at a time to get its total # unencrypted size size = 0 while !eof read_block - size += @read_buffer.size + size += @read_buffer.size @read_buffer = '' end rewind offset = size + amount else @@ -314,17 +314,19 @@ private # Read the header from the file if present def read_header - @pos = 0 + @pos = 0 # Read first block and check for the header - buf = @ios.read(@buffer_size) + buf = @ios.read(@buffer_size) # Use cipher specified in header, or global cipher if it has no header - iv, key, cipher_name, decryption_cipher = nil + iv, key = nil + cipher_name = nil + decryption_cipher = nil if header = SymmetricEncryption::Cipher.parse_header!(buf) @header_present = true @compressed = header.compressed decryption_cipher = header.decryption_cipher cipher_name = header.cipher_name || decryption_cipher.cipher_name @@ -338,10 +340,10 @@ end @stream_cipher = ::OpenSSL::Cipher.new(cipher_name) @stream_cipher.decrypt @stream_cipher.key = key || decryption_cipher.send(:key) - @stream_cipher.iv = iv || decryption_cipher.iv + @stream_cipher.iv = iv || decryption_cipher.iv # First call to #update should return an empty string anyway if buf && buf.length > 0 @read_buffer = @stream_cipher.update(buf) @read_buffer << @stream_cipher.final if @ios.eof?