Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New SumStream function reads from io.Reader #138

Merged
merged 2 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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