lib/symmetric_encryption/reader.rb in symmetric-encryption-4.3.1 vs lib/symmetric_encryption/reader.rb in symmetric-encryption-4.3.2
- old
+ new
@@ -1,6 +1,6 @@
-require 'openssl'
+require "openssl"
module SymmetricEncryption
# Read from encrypted files and other IO streams
#
# Features:
@@ -58,11 +58,11 @@
# csv.each {|row| p row}
# ensure
# csv.close if csv
# end
def self.open(file_name_or_stream, buffer_size: 16_384, **args, &block)
- ios = file_name_or_stream.is_a?(String) ? ::File.open(file_name_or_stream, 'rb') : file_name_or_stream
+ ios = file_name_or_stream.is_a?(String) ? ::File.open(file_name_or_stream, "rb") : file_name_or_stream
begin
file = new(ios, buffer_size: buffer_size, **args)
file = Zlib::GzipReader.new(file) if !file.eof? && file.compressed?
block ? block.call(file) : file
@@ -102,11 +102,11 @@
open(file_name_or_stream, &:eof?)
end
# Returns [true|false] whether the file contains the encryption header
def self.header_present?(file_name)
- ::File.open(file_name, 'rb') { |file| new(file).header_present? }
+ ::File.open(file_name, "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?
@@ -118,13 +118,13 @@
@ios = ios
@buffer_size = buffer_size
@version = version
@header_present = false
@closed = false
- @read_buffer = ''.b
+ @read_buffer = "".b
- raise(ArgumentError, 'Buffer size cannot be smaller than 128') unless @buffer_size >= 128
+ raise(ArgumentError, "Buffer size cannot be smaller than 128") unless @buffer_size >= 128
read_header
end
# Returns whether the stream being read is compressed
@@ -183,14 +183,14 @@
# length is omitted or is nil. length must be a non-negative integer or nil.
#
# At end of file, it returns nil if no more data is available, or the last
# remaining bytes
def read(length = nil, outbuf = nil)
- data = outbuf.to_s.clear
+ data = outbuf.nil? ? "" : outbuf.clear
remaining_length = length
- until remaining_length == 0 || eof?
+ until remaining_length&.zero? || eof?
read_block(remaining_length) if @read_buffer.empty?
if remaining_length && remaining_length < @read_buffer.length
data << @read_buffer.slice!(0, remaining_length)
else
@@ -207,11 +207,11 @@
# 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, 'End of file reached when trying to read a line')
+ gets(sep_string) || raise(EOFError, "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
@@ -224,12 +224,12 @@
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.empty? && eof?
data
end
@@ -308,11 +308,11 @@
# Read the header from the file if present
def read_header
@pos = 0
# Read first block and check for the header
- buf = @ios.read(@buffer_size, @output_buffer ||= ''.b)
+ buf = @ios.read(@buffer_size, @output_buffer ||= "".b)
# Use cipher specified in header, or global cipher if it has no header
iv, key, cipher_name, cipher = nil
header = Header.new
if header.parse!(buf)
@@ -338,11 +338,11 @@
decrypt(buf)
end
# Read a block of data and append the decrypted data in the read buffer
def read_block(length = nil)
- buf = @ios.read(length || @buffer_size, @output_buffer ||= ''.b)
+ buf = @ios.read(length || @buffer_size, @output_buffer ||= "".b)
decrypt(buf)
end
# Decrypts the given chunk of data and returns the result
if defined?(JRuby)
@@ -354,10 +354,10 @@
end
else
def decrypt(buf)
return if buf.nil? || buf.empty?
- @read_buffer << @stream_cipher.update(buf, @cipher_buffer ||= ''.b)
+ @read_buffer << @stream_cipher.update(buf, @cipher_buffer ||= "".b)
@read_buffer << @stream_cipher.final if @ios.eof?
end
end
def closed?