You are viewing documentation for version 2 of the AWS SDK for Ruby. Version 3 documentation can be found here.
Class: Aws::TreeHashUsed for computing a tree hash SHA256 checksum of an object.
tree_hash = TreeHash.new tree_hash.update(file.read(1024 * 1024)) until file.eof? tree_hash.digest
== Limitations and Notes
There are two main limitations to be aware of when using TreeHash:
TreeHash is not thread safe. Use multiple TreeHash objects to concurrently compute a tree hash of a large object. Join their hashes at the end into a single TreeHash and then call #digest
TreeHash.new(tree_hashes.map(&:hashes).flatten)
You must call #update with 1MB chunks of data. Only the final chunk may be smaller than 1MB.
If you have a large object/file, and you would like to compute the chunks concurrently, you must break the original file/data into sections that are evenly divisible by 1MB. Each section of data requires a seperate TreeHash object to compute hashes. Once all sections of data are complete, you can rejoin their #hashes in sequential order into a single TreeHash, then call #digest on the final tree hash.
Instance Attribute Summary collapseThe built up list of hashes.
A new instance of TreeHash.
Returns the computed SHA256 digest of the chunk.
Returns a new instance of TreeHash.
33 34 35 36
# File 'aws-sdk-core/lib/aws-sdk-core/tree_hash.rb', line 33 def initialize(hashes = []) @digest = OpenSSL::Digest.new('sha256') @hashes = hashes endInstance Attribute Details #hashes ⇒ Array<String>
Returns The built up list of hashes. Each hash is a sha255 digest of a 1MB chunk.
40 41 42
# File 'aws-sdk-core/lib/aws-sdk-core/tree_hash.rb', line 40 def hashes @hashes endInstance Method Details #digest ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# File 'aws-sdk-core/lib/aws-sdk-core/tree_hash.rb', line 50 def digest hashes = @hashes digest = OpenSSL::Digest.new('sha256') until hashes.count == 1 hashes = hashes.each_slice(2).map do |h1,h2| digest.reset if h2 digest.update(h1) digest.update(h2) digest.digest else h1 end end end hashes.first.bytes.map{|x| x.to_i.to_s(16).rjust(2, '0')}.join('') end#update(chunk) ⇒ String
Returns the computed SHA256 digest of the chunk.
44 45 46 47 48
# File 'aws-sdk-core/lib/aws-sdk-core/tree_hash.rb', line 44 def update(chunk) @hashes << @digest.update(chunk).digest @digest.reset @hashes.last end
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4