forked from aws/aws-sdk-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_hash.rb
69 lines (62 loc) · 2.04 KB
/
tree_hash.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'openssl'
module Aws
# Used 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.
#
class TreeHash
def initialize(hashes = [])
@digest = OpenSSL::Digest.new('sha256')
@hashes = hashes
end
# @return [Array<String>] The built up list of hashes. Each hash is
# a sha255 digest of a 1MB chunk.
attr_accessor :hashes
# @param [String] chunk
# @return [String] Returns the computed SHA256 digest of the chunk.
def update(chunk)
@hashes << @digest.update(chunk).digest
@digest.reset
@hashes.last
end
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
end
end