opentofu/vendor/github.com/aws/aws-sdk-go/service/glacier/treehash.go
Paul Hinze 6fe2703665 Vendor all dependencies w/ Godep
* Remove `make updatedeps` from Travis build. We'll follow up with more
   specific plans around dependency updating in subsequent PRs.
 * Update all `make` targets to set `GO15VENDOREXPERIMENT=1` and to
   filter out `/vendor/` from `./...` where appropriate.
 * Temporarily remove `vet` from the `make test` target until we can
   figure out how to get it to not vet `vendor/`. (Initial
   experimentation failed to yield the proper incantation.)

Everything is pinned to current master, with the exception of:

 * Azure/azure-sdk-for-go which is pinned before the breaking change today
 * aws/aws-sdk-go which is pinned to the most recent tag

The documentation still needs to be updated, which we can do in a follow
up PR. The goal here is to unblock release.
2016-01-29 15:08:48 -06:00

72 lines
1.5 KiB
Go

package glacier
import (
"crypto/sha256"
"io"
)
const bufsize = 1024 * 1024
// Hash contains information about the tree-hash and linear hash of a
// Glacier payload. This structure is generated by ComputeHashes().
type Hash struct {
TreeHash []byte
LinearHash []byte
}
// ComputeHashes computes the tree-hash and linear hash of a seekable reader r.
func ComputeHashes(r io.ReadSeeker) Hash {
r.Seek(0, 0) // Read the whole stream
defer r.Seek(0, 0) // Rewind stream at end
buf := make([]byte, bufsize)
hashes := [][]byte{}
hsh := sha256.New()
for {
// Build leaf nodes in 1MB chunks
n, err := io.ReadAtLeast(r, buf, bufsize)
if n == 0 {
break
}
tmpHash := sha256.Sum256(buf[:n])
hashes = append(hashes, tmpHash[:])
hsh.Write(buf[:n]) // Track linear hash while we're at it
if err != nil {
break // This is the last chunk
}
}
return Hash{
LinearHash: hsh.Sum(nil),
TreeHash: buildHashTree(hashes),
}
}
// buildHashTree builds a hash tree root node given a set of hashes.
func buildHashTree(hashes [][]byte) []byte {
if hashes == nil || len(hashes) == 0 {
return nil
}
for len(hashes) > 1 {
tmpHashes := [][]byte{}
for i := 0; i < len(hashes); i += 2 {
if i+1 <= len(hashes)-1 {
tmpHash := append(append([]byte{}, hashes[i]...), hashes[i+1]...)
tmpSum := sha256.Sum256(tmpHash)
tmpHashes = append(tmpHashes, tmpSum[:])
} else {
tmpHashes = append(tmpHashes, hashes[i])
}
}
hashes = tmpHashes
}
return hashes[0]
}