From 72fa77af98a26079bd965c7ca4a9e1ec8f181cdc Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Mon, 29 Apr 2024 11:45:19 +0200 Subject: [PATCH] io.Copy(io.Discard, h.reader) is a bit too lazy --- pkg/iotools/hashReaderWriter.go | 17 +++++++++++++++-- pkg/iotools/hashReaderWriter_test.go | 9 +++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/iotools/hashReaderWriter.go b/pkg/iotools/hashReaderWriter.go index 375c75376b..2db2b2b95c 100644 --- a/pkg/iotools/hashReaderWriter.go +++ b/pkg/iotools/hashReaderWriter.go @@ -46,8 +46,21 @@ func (h *HashReader) ReadAll() ([]byte, error) { return io.ReadAll(h.reader) } +// CalcHashes returns the total number of bytes read and an error if any besides EOF. func (h *HashReader) CalcHashes() (int64, error) { - return io.Copy(io.Discard, h.reader) + b := make([]byte, 0, 512) + cnt := int64(0) + for { + n, err := h.Read(b[0:cap(b)]) // read a chunk, always from the beginning + b = b[:n] // reset slice to the actual read bytes + cnt += int64(n) + if err != nil { + if err == io.EOF { + err = nil + } + return cnt, err + } + } } //////////////////////////////////////////////////////////////////////////////// @@ -106,7 +119,7 @@ func getBytes(h hashes, algorithm crypto.Hash) []byte { func httpHeader(h hashes) map[string]string { headers := make(map[string]string, len(h.hashes())) - for algorithm, _ := range h.hashes() { + for algorithm := range h.hashes() { headers[headerName(algorithm)] = getString(h, algorithm) } return headers diff --git a/pkg/iotools/hashReaderWriter_test.go b/pkg/iotools/hashReaderWriter_test.go index cc535f72e5..bda2a6fc3d 100644 --- a/pkg/iotools/hashReaderWriter_test.go +++ b/pkg/iotools/hashReaderWriter_test.go @@ -13,7 +13,6 @@ import ( ) var _ = Describe("Hash Reader Writer tests", func() { - It("Ensure interface implementation", func() { var _ io.Reader = &iotools.HashReader{} var _ io.Reader = (*iotools.HashReader)(nil) @@ -62,6 +61,13 @@ var _ = Describe("Hash Reader Writer tests", func() { Expect(hr.GetBytes(0)).To(BeNil()) Expect(hr.GetString(crypto.SHA1)).To(Equal("5c075ed604db0adc524edd3516e8f0258ca6e58d")) + hr = iotools.NewHashReader(strings.NewReader(s), crypto.SHA1) + cnt, err := hr.CalcHashes() + Expect(err).To(BeNil()) + Expect(cnt).To(Equal(int64(len(s)))) + Expect(hr.GetBytes(0)).To(BeNil()) + Expect(hr.GetString(crypto.SHA1)).To(Equal("5c075ed604db0adc524edd3516e8f0258ca6e58d")) + hr = iotools.NewHashReader(strings.NewReader(s), crypto.SHA1, crypto.MD5) hr.Read(buf) Expect(hr.GetBytes(crypto.SHA256)).To(BeNil()) @@ -70,5 +76,4 @@ var _ = Describe("Hash Reader Writer tests", func() { Expect(hr.GetString(crypto.SHA1)).To(Equal("5c075ed604db0adc524edd3516e8f0258ca6e58d")) Expect(hr.GetString(crypto.SHA1)).To(Equal("5c075ed604db0adc524edd3516e8f0258ca6e58d")) }) - })