verifiers.go 1.3 KB

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