Skip to content

Commit

Permalink
Merge pull request #138 from gammazero/read-from-stream
Browse files Browse the repository at this point in the history
New SumStream function reads from io.Reader
  • Loading branch information
warpfork committed Apr 6, 2021
2 parents c3ba253 + 707d9c2 commit 75ae368
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 1 addition & 7 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"sort"
"strings"

Expand Down Expand Up @@ -149,10 +148,5 @@ func (o *Options) Check(r io.Reader, h1 mh.Multihash) error {

// Multihash reads all the data in r and calculates its multihash.
func (o *Options) Multihash(r io.Reader) (mh.Multihash, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

return mh.Sum(b, o.AlgorithmCode, o.Length)
return mh.SumStream(r, o.AlgorithmCode, o.Length)
}
28 changes: 26 additions & 2 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package multihash
import (
"errors"
"fmt"
"hash"
"io"

mhreg "github.com/multiformats/go-multihash/core"
)
Expand All @@ -13,8 +15,8 @@ var ErrSumNotSupported = mhreg.ErrSumNotSupported
var ErrLenTooLarge = errors.New("requested length was too large for digest")

// Sum obtains the cryptographic sum of a given buffer. The length parameter
// indicates the length of the resulting digest and passing a negative value
// use default length values for the selected hash function.
// indicates the length of the resulting digest. Passing a negative value uses
// default length values for the selected hash function.
func Sum(data []byte, code uint64, length int) (Multihash, error) {
// Get the algorithm.
hasher, err := GetHasher(code)
Expand All @@ -25,6 +27,28 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
// Feed data in.
hasher.Write(data)

return encodeHash(hasher, code, length)
}

// SumStream obtains the cryptographic sum of a given stream. The length
// parameter indicates the length of the resulting digest. Passing a negative
// value uses default length values for the selected hash function.
func SumStream(r io.Reader, code uint64, length int) (Multihash, error) {
// Get the algorithm.
hasher, err := GetHasher(code)
if err != nil {
return nil, err
}

// Feed data in.
if _, err = io.Copy(hasher, r); err != nil {
return nil, err
}

return encodeHash(hasher, code, length)
}

func encodeHash(hasher hash.Hash, code uint64, length int) (Multihash, error) {
// Compute final hash.
// A new slice is allocated. FUTURE: see other comment below about allocation, and review together with this line to try to improve.
sum := hasher.Sum(nil)
Expand Down

0 comments on commit 75ae368

Please sign in to comment.