2015-03-05 05:26:56 +01:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"hash"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Digester calculates the digest of written data. It is functionally
|
|
|
|
// equivalent to hash.Hash but provides methods for returning the Digest type
|
|
|
|
// rather than raw bytes.
|
|
|
|
type Digester struct {
|
2015-03-24 08:04:45 +01:00
|
|
|
alg string
|
|
|
|
hash.Hash
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDigester create a new Digester with the given hashing algorithm and instance
|
|
|
|
// of that algo's hasher.
|
|
|
|
func NewDigester(alg string, h hash.Hash) Digester {
|
|
|
|
return Digester{
|
|
|
|
alg: alg,
|
2015-03-24 08:04:45 +01:00
|
|
|
Hash: h,
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCanonicalDigester is a convenience function to create a new Digester with
|
2015-03-24 08:04:45 +01:00
|
|
|
// our default settings.
|
2015-03-05 05:26:56 +01:00
|
|
|
func NewCanonicalDigester() Digester {
|
|
|
|
return NewDigester("sha256", sha256.New())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Digest returns the current digest for this digester.
|
|
|
|
func (d *Digester) Digest() Digest {
|
2015-03-24 08:04:45 +01:00
|
|
|
return NewDigest(d.alg, d.Hash)
|
|
|
|
}
|
|
|
|
|
2015-04-16 03:12:45 +02:00
|
|
|
// ResumableHash is the common interface implemented by all resumable hash
|
|
|
|
// functions.
|
|
|
|
type ResumableHash interface {
|
|
|
|
// ResumableHash is a superset of hash.Hash
|
|
|
|
hash.Hash
|
|
|
|
// Len returns the number of bytes written to the Hash so far.
|
|
|
|
Len() uint64
|
|
|
|
// State returns a snapshot of the state of the Hash.
|
|
|
|
State() ([]byte, error)
|
|
|
|
// Restore resets the Hash to the given state.
|
|
|
|
Restore(state []byte) error
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|
|
|
|
|
2015-04-16 03:12:45 +02:00
|
|
|
// ResumableDigester is a digester that can export its internal state and be
|
|
|
|
// restored from saved state.
|
|
|
|
type ResumableDigester interface {
|
|
|
|
ResumableHash
|
|
|
|
Digest() Digest
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|