1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 08:16:36 +00:00
restic/internal/hashing/reader.go
2022-05-11 21:36:10 +02:00

28 lines
542 B
Go

package hashing
import (
"hash"
"io"
)
// A Reader hashes all data read from the underlying reader.
type Reader struct {
r io.Reader
h hash.Hash
}
// NewReader returns a new Reader that uses the hash h.
func NewReader(r io.Reader, h hash.Hash) *Reader {
return &Reader{r: r, h: h}
}
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
}
// Sum returns the hash of the data read so far.
func (h *Reader) Sum(d []byte) []byte {
return h.h.Sum(d)
}