github.com/mckael/restic@v0.8.3/internal/hashing/writer.go (about) 1 package hashing 2 3 import ( 4 "hash" 5 "io" 6 ) 7 8 // Writer transparently hashes all data while writing it to the underlying writer. 9 type Writer struct { 10 w io.Writer 11 h hash.Hash 12 } 13 14 // NewWriter wraps the writer w and feeds all data written to the hash h. 15 func NewWriter(w io.Writer, h hash.Hash) *Writer { 16 return &Writer{ 17 h: h, 18 w: io.MultiWriter(w, h), 19 } 20 } 21 22 // Write wraps the write method of the underlying writer and also hashes all data. 23 func (h *Writer) Write(p []byte) (int, error) { 24 n, err := h.w.Write(p) 25 return n, err 26 } 27 28 // Sum returns the hash of all data written so far. 29 func (h *Writer) Sum(d []byte) []byte { 30 return h.h.Sum(d) 31 }