Sha256: 3c81ec8e525e2b921fc61b6778bb281bafb0ddd99582698244a6aebea97aacf1

Contents?: true

Size: 1.28 KB

Versions: 15

Compression:

Stored size: 1.28 KB

Contents

require 'openssl'
require 'tempfile'

module Aws
  # @api private
  module Checksums

    CHUNK_SIZE = 1 * 1024 * 1024 # one MB

    class << self

      # @param [File, Tempfile, IO#read, String] value
      # @return [String<SHA256 Hexdigest>]
      def sha256_hexdigest(value)
        if File === value || Tempfile === value
          OpenSSL::Digest::SHA256.file(value).hexdigest
        elsif value.respond_to?(:read)
          sha256 = OpenSSL::Digest::SHA256.new
          update_in_chunks(sha256, value)
          sha256.hexdigest
        else
          OpenSSL::Digest::SHA256.hexdigest(value)
        end
      end

      # @param [File, Tempfile, IO#read, String] value
      # @return [String<MD5>]
      def md5(value)
        if File === value || Tempfile === value
          Base64.encode64(OpenSSL::Digest::MD5.file(value).digest).strip
        elsif value.respond_to?(:read)
          md5 = OpenSSL::Digest::MD5.new
          update_in_chunks(md5, value)
          Base64.encode64(md5.digest).strip
        else
          Base64.encode64(OpenSSL::Digest::MD5.digest(value)).strip
        end
      end

      private

      def update_in_chunks(digest, io)
        while chunk = io.read(CHUNK_SIZE)
          digest.update(chunk)
        end
        io.rewind
      end

    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
aws-sdk-core-2.3.23 lib/aws-sdk-core/checksums.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/aws-sdk-core-2.3.22/lib/aws-sdk-core/checksums.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/aws-sdk-core-2.3.22/lib/aws-sdk-core/checksums.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/aws-sdk-core-2.3.22/lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.4.2 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.4.1 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.4.0 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.22 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.21 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.20 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.19 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.18 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.17 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.16 lib/aws-sdk-core/checksums.rb
aws-sdk-core-2.3.15 lib/aws-sdk-core/checksums.rb