class Gem::Package::DigestIO

IO 包装器,用于创建写入到其包装的 IO 的内容的摘要。

属性

digests[R]

收集的包装写入的摘要。

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

公共类方法

new(io, digests) 点击以切换源代码

创建一个新的 DigestIO 实例。建议使用 ::wrap,请参阅 ::wrap 文档以了解 iodigests 的文档。

# File rubygems/package/digest_io.rb, line 46
def initialize(io, digests)
  @io = io
  @digests = digests
end
wrap(io, digests) { |digest_io| ... } 点击以切换源代码

包装 io 并为 digests 哈希中的每个摘要算法更新摘要。返回摘要哈希。示例

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[...]"
# File rubygems/package/digest_io.rb, line 34
def self.wrap(io, digests)
  digest_io = new io, digests

  yield digest_io

  digests
end

公共实例方法

write(data) 点击以切换源代码

data 写入底层 IO 并更新摘要

# File rubygems/package/digest_io.rb, line 54
def write(data)
  result = @io.write data

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

  result
end