class Aws::S3::EncryptionV2::IOEncrypter

Provides an IO wrapper encrypting a stream of data. @api private

Constants

ONE_MEGABYTE

@api private

Attributes

size[R]

@return [Integer]

Public Class Methods

new(cipher, io) click to toggle source
# File lib/aws-sdk-s3/encryptionV2/io_encrypter.rb, line 17
def initialize(cipher, io)
  @encrypted = io.size <= ONE_MEGABYTE ?
    encrypt_to_stringio(cipher, io.read) :
    encrypt_to_tempfile(cipher, io)
  @size = @encrypted.size
end

Public Instance Methods

close() click to toggle source

@api private

# File lib/aws-sdk-s3/encryptionV2/io_encrypter.rb, line 40
def close
  @encrypted.close if @encrypted.is_a?(Tempfile)
end
read(bytes = nil, output_buffer = nil) click to toggle source
# File lib/aws-sdk-s3/encryptionV2/io_encrypter.rb, line 27
def read(bytes = nil, output_buffer = nil)
  if @encrypted.is_a?(Tempfile) && @encrypted.closed?
    @encrypted.open
    @encrypted.binmode
  end
  @encrypted.read(bytes, output_buffer)
end
rewind() click to toggle source
# File lib/aws-sdk-s3/encryptionV2/io_encrypter.rb, line 35
def rewind
  @encrypted.rewind
end

Private Instance Methods

encrypt_to_stringio(cipher, plain_text) click to toggle source
# File lib/aws-sdk-s3/encryptionV2/io_encrypter.rb, line 46
def encrypt_to_stringio(cipher, plain_text)
  if plain_text.empty?
    StringIO.new(cipher.final + cipher.auth_tag)
  else
    StringIO.new(cipher.update(plain_text) + cipher.final + cipher.auth_tag)
  end
end
encrypt_to_tempfile(cipher, io) click to toggle source
# File lib/aws-sdk-s3/encryptionV2/io_encrypter.rb, line 54
def encrypt_to_tempfile(cipher, io)
  encrypted = Tempfile.new(self.object_id.to_s)
  encrypted.binmode
  while chunk = io.read(ONE_MEGABYTE, read_buffer ||= String.new)
    if cipher.method(:update).arity == 1
      encrypted.write(cipher.update(chunk))
    else
      encrypted.write(cipher.update(chunk, cipher_buffer ||= String.new))
    end
  end
  encrypted.write(cipher.final)
  encrypted.write(cipher.auth_tag)
  encrypted.rewind
  encrypted
end