2015-03-05 05:26:56 +01:00
|
|
|
package digest
|
|
|
|
|
2015-05-21 08:44:08 +02:00
|
|
|
import "hash"
|
2015-03-05 05:26:56 +01:00
|
|
|
|
2015-05-21 08:44:08 +02:00
|
|
|
// Digester calculates the digest of written data. Writes should go directly
|
|
|
|
// to the return value of Hash, while calling Digest will return the current
|
|
|
|
// value of the digest.
|
|
|
|
type Digester interface {
|
|
|
|
Hash() hash.Hash // provides direct access to underlying hash instance.
|
|
|
|
Digest() Digest
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:44:08 +02:00
|
|
|
// NewDigester create a new Digester with the given hashing algorithm and
|
|
|
|
// instance of that algo's hasher.
|
2015-03-05 05:26:56 +01:00
|
|
|
func NewDigester(alg string, h hash.Hash) Digester {
|
2015-05-21 08:44:08 +02:00
|
|
|
return &digester{
|
2015-03-05 05:26:56 +01:00
|
|
|
alg: alg,
|
2015-05-21 08:44:08 +02: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 {
|
2015-05-21 08:44:08 +02:00
|
|
|
return NewDigester(CanonicalAlgorithm, CanonicalHash.New())
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:44:08 +02:00
|
|
|
// digester provides a simple digester definition that embeds a hasher.
|
|
|
|
type digester struct {
|
|
|
|
alg string
|
|
|
|
hash hash.Hash
|
2015-03-24 08:04:45 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:44:08 +02:00
|
|
|
func (d *digester) Hash() hash.Hash {
|
|
|
|
return d.hash
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 08:44:08 +02:00
|
|
|
func (d *digester) Digest() Digest {
|
|
|
|
return NewDigest(d.alg, d.Hash())
|
2015-03-05 05:26:56 +01:00
|
|
|
}
|