Class: Fog::AWS::Glacier::TreeHash

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/glacier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTreeHash

Returns a new instance of TreeHash.



55
56
57
# File 'lib/fog/aws/glacier.rb', line 55

def initialize
  @digests = []
end

Class Method Details

.digest(body) ⇒ Object



38
39
40
# File 'lib/fog/aws/glacier.rb', line 38

def self.digest(body)
  new.add_part(body)
end

Instance Method Details

#add_part(bytes) ⇒ Object



59
60
61
62
63
# File 'lib/fog/aws/glacier.rb', line 59

def add_part(bytes)
  part = self.digest_for_part(bytes)
  @digests << part
  part.unpack('H*').first
end

#digestObject



86
87
88
# File 'lib/fog/aws/glacier.rb', line 86

def digest
  reduce_digests(@digests)
end

#digest_for_part(body) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fog/aws/glacier.rb', line 65

def digest_for_part(body)
  chunk_count = [body.bytesize / MEGABYTE + (body.bytesize % MEGABYTE > 0 ? 1 : 0), 1].max
  if body.respond_to? :byteslice
    digests_for_part = chunk_count.times.map {|chunk_index| OpenSSL::Digest::SHA256.digest(body.byteslice(chunk_index * MEGABYTE, MEGABYTE))}
  else
    if body.respond_to? :encoding
      old_encoding = body.encoding
      body.force_encoding('BINARY')
    end
    digests_for_part = chunk_count.times.map {|chunk_index| OpenSSL::Digest::SHA256.digest(body.slice(chunk_index * MEGABYTE, MEGABYTE))}
    if body.respond_to? :encoding
      body.force_encoding(old_encoding)
    end
  end
  reduce_digests(digests_for_part)
end

#hexdigestObject



82
83
84
# File 'lib/fog/aws/glacier.rb', line 82

def hexdigest
  digest.unpack('H*').first
end

#reduce_digests(digests) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aws/glacier.rb', line 42

def reduce_digests(digests)
  while digests.length > 1
    digests = digests.each_slice(2).map do |pair|
      if pair.length == 2
        OpenSSL::Digest::SHA256.digest(pair[0]+pair[1])
      else
        pair.first
      end
    end
  end
  digests.first
end