| 
									
										
										
										
											2017-01-22 12:43:36 +01:00
										 |  |  | package hashing | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"hash" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Writer transparently hashes all data while writing it to the underlying writer. | 
					
						
							|  |  |  | type Writer struct { | 
					
						
							|  |  |  | 	w io.Writer | 
					
						
							|  |  |  | 	h hash.Hash | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewWriter wraps the writer w and feeds all data written to the hash h. | 
					
						
							|  |  |  | func NewWriter(w io.Writer, h hash.Hash) *Writer { | 
					
						
							|  |  |  | 	return &Writer{ | 
					
						
							|  |  |  | 		h: h, | 
					
						
							| 
									
										
										
										
											2020-03-05 21:06:16 +01:00
										 |  |  | 		w: w, | 
					
						
							| 
									
										
										
										
											2017-01-22 12:43:36 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Write wraps the write method of the underlying writer and also hashes all data. | 
					
						
							|  |  |  | func (h *Writer) Write(p []byte) (int, error) { | 
					
						
							|  |  |  | 	n, err := h.w.Write(p) | 
					
						
							| 
									
										
										
										
											2020-03-05 21:06:16 +01:00
										 |  |  | 	h.h.Write(p[:n]) | 
					
						
							| 
									
										
										
										
											2017-01-22 12:43:36 +01:00
										 |  |  | 	return n, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Sum returns the hash of all data written so far. | 
					
						
							|  |  |  | func (h *Writer) Sum(d []byte) []byte { | 
					
						
							|  |  |  | 	return h.h.Sum(d) | 
					
						
							|  |  |  | } |