lib/symmetric_encryption/writer.rb in symmetric-encryption-4.0.0 vs lib/symmetric_encryption/writer.rb in symmetric-encryption-4.0.1

- old
+ new

@@ -49,11 +49,11 @@ # end def self.open(file_name_or_stream, compress: false, **args) ios = file_name_or_stream.is_a?(String) ? ::File.open(file_name_or_stream, 'wb') : file_name_or_stream begin - file = self.new(ios, compress: compress, **args) + file = new(ios, compress: compress, **args) file = Zlib::GzipWriter.new(file) if compress block_given? ? yield(file) : file ensure file.close if block_given? && file && (file.respond_to?(:closed?) && !file.closed?) end @@ -62,11 +62,11 @@ # Write the contents of a string in memory to an encrypted file / stream. # # Notes: # * Do not use this method for writing large files. def self.write(file_name_or_stream, data, **args) - open(file_name_or_stream, **args) { |f| f.write(data) } + self.open(file_name_or_stream, **args) { |f| f.write(data) } end # Encrypt an entire file. # # Returns [Integer] the number of encrypted bytes written to the target file. @@ -87,21 +87,19 @@ # For very large files using a larger block size is faster. # Default: 65535 # # Notes: # * The file contents are streamed so that the entire file is _not_ loaded into memory. - def self.encrypt(source:, target:, block_size: 65535, **args) + def self.encrypt(source:, target:, block_size: 65_535, **args) source_ios = source.is_a?(String) ? ::File.open(source, 'rb') : source bytes_written = 0 - open(target, **args) do |output_file| - while !source_ios.eof? - bytes_written += output_file.write(source_ios.read(block_size)) - end + self.open(target, **args) do |output_file| + bytes_written += output_file.write(source_ios.read(block_size)) until source_ios.eof? end bytes_written ensure - source_ios.close if source_ios && source_ios.respond_to?(:closed?) && !source_ios.closed? + source_ios.close if source_ios&.respond_to?(:closed?) && !source_ios.closed? end # Encrypt data before writing to the supplied stream def initialize(ios, version: nil, cipher_name: nil, header: true, random_key: true, random_iv: true, compress: false) # Compress is only used at this point for setting the flag in the header @@ -112,13 +110,11 @@ # Cipher to encrypt the random_key, or the entire file cipher = SymmetricEncryption.cipher(version) raise(SymmetricEncryption::CipherError, "Cipher with version:#{version} not found in any of the configured SymmetricEncryption ciphers") unless cipher # Force header if compressed or using random iv, key - if (header == true) || compress || random_key || random_iv - header = Header.new(version: cipher.version, compress: compress, cipher_name: cipher_name) - end + header = Header.new(version: cipher.version, compress: compress, cipher_name: cipher_name) if (header == true) || compress || random_key || random_iv @stream_cipher = ::OpenSSL::Cipher.new(cipher_name || cipher.cipher_name) @stream_cipher.encrypt if random_key @@ -127,12 +123,12 @@ @stream_cipher.key = cipher.send(:key) end if random_iv header.iv = @stream_cipher.iv = @stream_cipher.random_iv - else - @stream_cipher.iv = cipher.iv if cipher.iv + elsif cipher.iv + @stream_cipher.iv = cipher.iv end @ios.write(header.to_s) if header @size = 0 @@ -151,13 +147,13 @@ # It is recommended to call Symmetric::EncryptedStream.open # rather than creating an instance of Symmetric::Writer directly to # ensure that the encrypted stream is closed before the stream itself is closed. def close(close_child_stream = true) return if closed? - if size > 0 + if size.positive? final = @stream_cipher.final - @ios.write(final) if final.length > 0 + @ios.write(final) unless final.empty? end @ios.close if close_child_stream @closed = true end @@ -168,11 +164,11 @@ return unless data bytes = data.to_s @size += bytes.size partial = @stream_cipher.update(bytes) - @ios.write(partial) if partial.length > 0 + @ios.write(partial) unless partial.empty? data.length end # Write to the IO Stream as encrypted data. # @@ -199,8 +195,7 @@ end # Returns [Integer] the number of unencrypted and uncompressed bytes # written to the file so far. attr_reader :size - end end