2014-11-19 22:23:01 +01:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2014-11-19 23:59:05 +01: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 22:23:01 +01:00
|
|
|
type Verifier interface {
|
|
|
|
io.Writer
|
|
|
|
|
|
|
|
// Verified will return true if the content written to Verifier matches
|
|
|
|
// the digest.
|
|
|
|
Verified() bool
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:06:18 +01:00
|
|
|
// NewDigestVerifier is deprecated. Please use Digest.Verifier.
|
2015-03-10 22:40:58 +01:00
|
|
|
func NewDigestVerifier(d Digest) (Verifier, error) {
|
|
|
|
if err := d.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-16 02:18:13 +01:00
|
|
|
return hashVerifier{
|
|
|
|
hash: d.Algorithm().Hash(),
|
|
|
|
digest: d,
|
|
|
|
}, nil
|
2014-11-19 22:23:01 +01: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)
|
|
|
|
}
|