github.com/npaton/distribution@v2.3.1-rc.0+incompatible/digest/verifiers.go (about) 1 package digest 2 3 import ( 4 "hash" 5 "io" 6 ) 7 8 // Verifier presents a general verification interface to be used with message 9 // digests and other byte stream verifications. Users instantiate a Verifier 10 // from one of the various methods, write the data under test to it then check 11 // the result with the Verified method. 12 type Verifier interface { 13 io.Writer 14 15 // Verified will return true if the content written to Verifier matches 16 // the digest. 17 Verified() bool 18 } 19 20 // NewDigestVerifier returns a verifier that compares the written bytes 21 // against a passed in digest. 22 func NewDigestVerifier(d Digest) (Verifier, error) { 23 if err := d.Validate(); err != nil { 24 return nil, err 25 } 26 27 return hashVerifier{ 28 hash: d.Algorithm().Hash(), 29 digest: d, 30 }, nil 31 } 32 33 type hashVerifier struct { 34 digest Digest 35 hash hash.Hash 36 } 37 38 func (hv hashVerifier) Write(p []byte) (n int, err error) { 39 return hv.hash.Write(p) 40 } 41 42 func (hv hashVerifier) Verified() bool { 43 return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash) 44 }