Sha256: bafcd64c68a4bc2ef68be1306367619eb723583e88977e62d7d3761cff40f5b0

Contents?: true

Size: 1.83 KB

Versions: 212

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require 'stringio'
require 'tempfile'

module Aws
  module S3
    module EncryptionV2

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

        # @api private
        ONE_MEGABYTE = 1024 * 1024

        def initialize(cipher, io)
          @encrypted = io.size <= ONE_MEGABYTE ?
            encrypt_to_stringio(cipher, io.read) :
            encrypt_to_tempfile(cipher, io)
          @size = @encrypted.size
        end

        # @return [Integer]
        attr_reader :size

        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

        def rewind
          @encrypted.rewind
        end

        # @api private
        def close
          @encrypted.close if @encrypted.is_a?(Tempfile)
        end

        private

        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

        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

      end
    end
  end
end

Version data entries

212 entries across 212 versions & 2 rubygems

Version Path
aws-sdk-s3-1.182.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.181.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.180.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.179.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.178.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.177.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.176.1 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.176.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.175.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.174.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.173.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.172.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.171.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.170.1 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.170.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.169.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.168.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.167.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.166.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb
aws-sdk-s3-1.165.0 lib/aws-sdk-s3/encryptionV2/io_encrypter.rb