2017-01-22 11:43:36 +00:00
|
|
|
package hashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2022-05-11 19:36:10 +00:00
|
|
|
// A Reader hashes all data read from the underlying reader.
|
2022-05-10 20:35:57 +00:00
|
|
|
type Reader struct {
|
|
|
|
r io.Reader
|
2017-01-22 11:43:36 +00:00
|
|
|
h hash.Hash
|
|
|
|
}
|
|
|
|
|
2022-05-11 19:36:10 +00:00
|
|
|
// NewReader returns a new Reader that uses the hash h.
|
2022-05-10 20:35:57 +00:00
|
|
|
func NewReader(r io.Reader, h hash.Hash) *Reader {
|
|
|
|
return &Reader{r: r, h: h}
|
|
|
|
}
|
2021-01-01 11:42:33 +00:00
|
|
|
|
2022-05-10 20:35:57 +00:00
|
|
|
func (h *Reader) Read(p []byte) (int, error) {
|
|
|
|
n, err := h.r.Read(p)
|
|
|
|
_, _ = h.h.Write(p[:n]) // Never returns an error.
|
|
|
|
return n, err
|
2017-01-22 11:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sum returns the hash of the data read so far.
|
2022-05-10 20:35:57 +00:00
|
|
|
func (h *Reader) Sum(d []byte) []byte {
|
2017-01-22 11:43:36 +00:00
|
|
|
return h.h.Sum(d)
|
|
|
|
}
|