github.com/mckael/restic@v0.8.3/internal/hashing/reader.go (about)

     1  package hashing
     2  
     3  import (
     4  	"hash"
     5  	"io"
     6  )
     7  
     8  // Reader hashes all data read from the underlying reader.
     9  type Reader struct {
    10  	r io.Reader
    11  	h hash.Hash
    12  }
    13  
    14  // NewReader returns a new Reader that uses the hash h.
    15  func NewReader(r io.Reader, h hash.Hash) *Reader {
    16  	return &Reader{
    17  		h: h,
    18  		r: io.TeeReader(r, h),
    19  	}
    20  }
    21  
    22  func (h *Reader) Read(p []byte) (int, error) {
    23  	return h.r.Read(p)
    24  }
    25  
    26  // Sum returns the hash of the data read so far.
    27  func (h *Reader) Sum(d []byte) []byte {
    28  	return h.h.Sum(d)
    29  }