lib/symmetric_encryption/writer.rb in symmetric-encryption-4.3.1 vs lib/symmetric_encryption/writer.rb in symmetric-encryption-4.3.2
- old
+ new
@@ -1,6 +1,6 @@
-require 'openssl'
+require "openssl"
module SymmetricEncryption
# Write to encrypted files and other IO streams.
#
# Features:
@@ -47,11 +47,11 @@
# ensure
# csv.close if csv
# end
def self.open(file_name_or_stream, compress: nil, **args)
if file_name_or_stream.is_a?(String)
- file_name_or_stream = ::File.open(file_name_or_stream, 'wb')
+ file_name_or_stream = ::File.open(file_name_or_stream, "wb")
compress = !(/\.(zip|gz|gzip|xls.|)\z/i === file_name_or_stream) if compress.nil?
else
compress = true if compress.nil?
end
@@ -95,19 +95,25 @@
# 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
@ios = ios
- raise(ArgumentError, 'When :random_key is true, :random_iv must also be true') if random_key && !random_iv
- raise(ArgumentError, 'Cannot supply a :cipher_name unless both :random_key and :random_iv are true') if cipher_name && !random_key && !random_iv
+ raise(ArgumentError, "When :random_key is true, :random_iv must also be true") if random_key && !random_iv
+ if cipher_name && !random_key && !random_iv
+ raise(ArgumentError, "Cannot supply a :cipher_name unless both :random_key and :random_iv are true")
+ end
# 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
+ unless cipher
+ raise(SymmetricEncryption::CipherError, "Cipher with version:#{version} not found in any of the configured SymmetricEncryption ciphers")
+ end
# Force header if compressed or using random iv, key
- header = Header.new(version: cipher.version, compress: compress, cipher_name: cipher_name) if (header == true) || compress || random_key || random_iv
+ if (header == true) || compress || random_key || random_iv
+ header = Header.new(version: cipher.version, compress: compress, cipher_name: cipher_name)
+ end
@stream_cipher = ::OpenSSL::Cipher.new(cipher_name || cipher.cipher_name)
@stream_cipher.encrypt
if random_key
@@ -156,22 +162,22 @@
# Returns [Integer] the number of bytes written.
if defined?(JRuby)
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) unless partial.empty?
data.length
end
else
def write(data)
return unless data
- bytes = data.to_s
- @size += bytes.size
- partial = @stream_cipher.update(bytes, @cipher_buffer ||= ''.b)
+ bytes = data.to_s
+ @size += bytes.size
+ partial = @stream_cipher.update(bytes, @cipher_buffer ||= "".b)
@ios.write(partial) unless partial.empty?
data.length
end
end