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.



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

def initialize
  @digests = []
end

Class Method Details

.digest(body) ⇒ Object



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

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

Instance Method Details

#add_part(bytes) ⇒ Object



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

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

#digestObject



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

def digest
  reduce_digests(@digests)
end

#digest_for_part(body) ⇒ Object



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

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| 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| 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



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

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

#reduce_digests(digests) ⇒ Object



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

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