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.



58
59
60
# File 'lib/fog/aws/glacier.rb', line 58

def initialize
  @digests = []
end

Class Method Details

.digest(body) ⇒ Object



41
42
43
# File 'lib/fog/aws/glacier.rb', line 41

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

Instance Method Details

#add_part(bytes) ⇒ Object



62
63
64
65
66
# File 'lib/fog/aws/glacier.rb', line 62

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

#digestObject



89
90
91
# File 'lib/fog/aws/glacier.rb', line 89

def digest
  reduce_digests(@digests)
end

#digest_for_part(body) ⇒ Object



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

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.collect {|chunk_index| 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.collect {|chunk_index| 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



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

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

#reduce_digests(digests) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/aws/glacier.rb', line 45

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