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 16 package digest 17 18 import ( 19 "hash" 20 "io" 21 ) 22 23 // Verifier presents a general verification interface to be used with message 24 // digests and other byte stream verifications. Users instantiate a Verifier 25 // from one of the various methods, write the data under test to it then check 26 // the result with the Verified method. 27 type Verifier interface { 28 io.Writer 29 30 // Verified will return true if the content written to Verifier matches 31 // the digest. 32 Verified() bool 33 } 34 35 type hashVerifier struct { 36 digest Digest 37 hash hash.Hash 38 } 39 40 func (hv hashVerifier) Write(p []byte) (n int, err error) { 41 return hv.hash.Write(p) 42 } 43 44 func (hv hashVerifier) Verified() bool { 45 return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash) 46 } 47