lib/symmetric_encryption/writer.rb in symmetric-encryption-3.7.2 vs lib/symmetric_encryption/writer.rb in symmetric-encryption-3.8.0
- old
+ new
@@ -98,13 +98,13 @@
# ensure
# csv.close if csv
# end
def self.open(filename_or_stream, options={}, &block)
raise(ArgumentError, 'options must be a hash') unless options.respond_to?(:each_pair)
- mode = options.fetch(:mode, 'wb')
+ mode = options.fetch(:mode, 'wb')
compress = options.fetch(:compress, false)
- ios = filename_or_stream.is_a?(String) ? ::File.open(filename_or_stream, mode) : filename_or_stream
+ ios = filename_or_stream.is_a?(String) ? ::File.open(filename_or_stream, mode) : filename_or_stream
begin
file = self.new(ios, options)
file = Zlib::GzipWriter.new(file) if compress
block ? block.call(file) : file
@@ -112,15 +112,15 @@
file.close if block && file && (file.respond_to?(:closed?) && !file.closed?)
end
end
# Encrypt data before writing to the supplied stream
- def initialize(ios,options={})
- @ios = ios
- header = options.fetch(:header, true)
- random_key = options.fetch(:random_key, true)
- random_iv = options.fetch(:random_iv, random_key)
+ def initialize(ios, options={})
+ @ios = ios
+ header = options.fetch(:header, true)
+ random_key = options.fetch(:random_key, true)
+ random_iv = options.fetch(:random_iv, random_key)
raise(ArgumentError, 'When :random_key is true, :random_iv must also be true') if random_key && !random_iv
# Compress is only used at this point for setting the flag in the header
compress = options.fetch(:compress, false)
version = options[:version]
cipher_name = options[:cipher_name]
@@ -135,25 +135,25 @@
@stream_cipher = ::OpenSSL::Cipher.new(cipher_name || cipher.cipher_name)
@stream_cipher.encrypt
key = random_key ? @stream_cipher.random_key : cipher.send(:key)
- iv = random_iv ? @stream_cipher.random_iv : cipher.send(:iv)
+ iv = random_iv ? @stream_cipher.random_iv : cipher.send(:iv)
@stream_cipher.key = key
- @stream_cipher.iv = iv if iv
+ @stream_cipher.iv = iv if iv
# Write the Encryption header including the random iv, key, and cipher
if header
@ios.write(Cipher.build_header(
cipher.version,
compress,
- random_iv ? iv : nil,
+ random_iv ? iv : nil,
random_key ? key : nil,
cipher_name))
end
- @size = 0
+ @size = 0
@closed = false
end
# Close the IO Stream
# Flushes any unwritten data
@@ -180,11 +180,11 @@
# Write to the IO Stream as encrypted data
# Returns the number of bytes written
def write(data)
return unless data
- bytes = data.to_s
- @size += bytes.size
+ bytes = data.to_s
+ @size += bytes.size
partial = @stream_cipher.update(bytes)
@ios.write(partial) if partial.length > 0
data.length
end