Class: Gem::Package::DigestIO

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/package/digest_io.rb

Overview

IO wrapper that creates digests of contents written to the IO it wraps.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, digests) ⇒ DigestIO

Creates a new DigestIO instance. Using ::wrap is recommended, see the ::wrap documentation for documentation of io and digests.



46
47
48
49
# File 'lib/rubygems/package/digest_io.rb', line 46

def initialize(io, digests)
  @io = io
  @digests = digests
end

Instance Attribute Details

#digestsObject (readonly)

Collected digests for wrapped writes.

{
  'SHA1'   => #<OpenSSL::Digest: [...]>,
  'SHA512' => #<OpenSSL::Digest: [...]>,
}


15
16
17
# File 'lib/rubygems/package/digest_io.rb', line 15

def digests
  @digests
end

Class Method Details

.wrap(io, digests) {|digest_io| ... } ⇒ Object

Wraps io and updates digest for each of the digest algorithms in the digests Hash. Returns the digests hash. Example:

io = StringIO.new
digests = {
  'SHA1'   => OpenSSL::Digest.new('SHA1'),
  'SHA512' => OpenSSL::Digest.new('SHA512'),
}

Gem::Package::DigestIO.wrap io, digests do |digest_io|
  digest_io.write "hello"
end

digests['SHA1'].hexdigest   #=> "aaf4c61d[...]"
digests['SHA512'].hexdigest #=> "9b71d224[...]"

Yields:

  • (digest_io)


34
35
36
37
38
39
40
# File 'lib/rubygems/package/digest_io.rb', line 34

def self.wrap(io, digests)
  digest_io = new io, digests

  yield digest_io

  digests
end

Instance Method Details

#write(data) ⇒ Object

Writes data to the underlying IO and updates the digests



54
55
56
57
58
59
60
61
62
# File 'lib/rubygems/package/digest_io.rb', line 54

def write(data)
  result = @io.write data

  @digests.each do |_, digest|
    digest << data
  end

  result
end