David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 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 | type hashVerifier struct { |
| 21 | digest Digest |
| 22 | hash hash.Hash |
| 23 | } |
| 24 | |
| 25 | func (hv hashVerifier) Write(p []byte) (n int, err error) { |
| 26 | return hv.hash.Write(p) |
| 27 | } |
| 28 | |
| 29 | func (hv hashVerifier) Verified() bool { |
| 30 | return hv.digest == NewDigest(hv.digest.Algorithm(), hv.hash) |
| 31 | } |