verifiers.go 720 B

1234567891011121314151617181920212223242526272829303132
  1. package digest
  2. import (
  3. "hash"
  4. "io"
  5. )
  6. // Verifier presents a general verification interface to be used with message
  7. // digests and other byte stream verifications. Users instantiate a Verifier
  8. // from one of the various methods, write the data under test to it then check
  9. // the result with the Verified method.
  10. type Verifier interface {
  11. io.Writer
  12. // Verified will return true if the content written to Verifier matches
  13. // the digest.
  14. Verified() bool
  15. }
  16. type hashVerifier struct {
  17. digest Digest
  18. hash hash.Hash
  19. }
  20. func (hv hashVerifier) Write(p []byte) (n int, err error) {
  21. return hv.hash.Write(p)
  22. }
  23. func (hv hashVerifier) Verified() bool {
  24. return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash)
  25. }