Sha256: 8340cfa92ce03bd4cef19fd3dab156a5bc4c09711d27b20246dc6c20d60940d4

Contents?: true

Size: 1.66 KB

Versions: 208

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Aws
  module S3
    module EncryptionV2
      # @api private
      class IOAuthDecrypter

        # @option options [required, IO#write] :io
        #   An IO-like object that responds to {#write}.
        # @option options [required, Integer] :encrypted_content_length
        #   The number of bytes to decrypt from the `:io` object.
        #   This should be the total size of `:io` minus the length of
        #   the cipher auth tag.
        # @option options [required, OpenSSL::Cipher] :cipher An initialized
        #   cipher that can be used to decrypt the bytes as they are
        #   written to the `:io` object. The cipher should already have
        #   its `#auth_tag` set.
        def initialize(options = {})
          @decrypter = IODecrypter.new(options[:cipher], options[:io])
          @max_bytes = options[:encrypted_content_length]
          @bytes_written = 0
        end

        def write(chunk)
          chunk = truncate_chunk(chunk)
          if chunk.bytesize > 0
            @bytes_written += chunk.bytesize
            @decrypter.write(chunk)
          end
        end

        def finalize
          @decrypter.finalize
        end

        def io
          @decrypter.io
        end

        private

        def truncate_chunk(chunk)
          if chunk.bytesize + @bytes_written <= @max_bytes
            chunk
          elsif @bytes_written < @max_bytes
            chunk[0..(@max_bytes - @bytes_written - 1)]
          else
            # If the tag was sent over after the full body has been read,
            # we don't want to accidentally append it.
            ""
          end
        end

      end
    end
  end
end

Version data entries

208 entries across 208 versions & 2 rubygems

Version Path
aws-sdk-s3-1.178.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.177.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.176.1 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.176.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.175.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.174.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.173.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.172.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.171.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.170.1 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.170.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.169.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.168.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.167.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.166.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.165.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.164.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.163.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.162.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb
aws-sdk-s3-1.161.0 lib/aws-sdk-s3/encryptionV2/io_auth_decrypter.rb