2014-11-19 21:23:01 +00:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2014-11-19 22:59:05 +00:00
|
|
|
// Verifier presents a general verification interface to be used with message
|
|
|
|
// digests and other byte stream verifications. Users instantiate a Verifier
|
|
|
|
// from one of the various methods, write the data under test to it then check
|
|
|
|
// the result with the Verified method.
|
2014-11-19 21:23:01 +00:00
|
|
|
type Verifier interface {
|
|
|
|
io.Writer
|
|
|
|
|
|
|
|
// Verified will return true if the content written to Verifier matches
|
|
|
|
// the digest.
|
|
|
|
Verified() bool
|
|
|
|
}
|
|
|
|
|
2016-12-15 23:06:18 +00:00
|
|
|
// NewDigestVerifier is deprecated. Please use Digest.Verifier.
|
2015-03-10 21:40:58 +00:00
|
|
|
func NewDigestVerifier(d Digest) (Verifier, error) {
|
|
|
|
if err := d.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-16 01:18:13 +00:00
|
|
|
return hashVerifier{
|
|
|
|
hash: d.Algorithm().Hash(),
|
|
|
|
digest: d,
|
|
|
|
}, nil
|
2014-11-19 21:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type hashVerifier struct {
|
|
|
|
digest Digest
|
|
|
|
hash hash.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hv hashVerifier) Write(p []byte) (n int, err error) {
|
|
|
|
return hv.hash.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hv hashVerifier) Verified() bool {
|
|
|
|
return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash)
|
|
|
|
}
|