verifiers.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2017 Docker, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package digest
  15. import (
  16. "hash"
  17. "io"
  18. )
  19. // Verifier presents a general verification interface to be used with message
  20. // digests and other byte stream verifications. Users instantiate a Verifier
  21. // from one of the various methods, write the data under test to it then check
  22. // the result with the Verified method.
  23. type Verifier interface {
  24. io.Writer
  25. // Verified will return true if the content written to Verifier matches
  26. // the digest.
  27. Verified() bool
  28. }
  29. type hashVerifier struct {
  30. digest Digest
  31. hash hash.Hash
  32. }
  33. func (hv hashVerifier) Write(p []byte) (n int, err error) {
  34. return hv.hash.Write(p)
  35. }
  36. func (hv hashVerifier) Verified() bool {
  37. return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash)
  38. }