Skip to content

fix: only register one blake2s length #129

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

Merged
merged 2 commits into from
May 13, 2020
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
14 changes: 4 additions & 10 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
return Encode(d, code)
}

func sumBlake2s(data []byte, size int) ([]byte, error) {
if size != 32 {
return nil, fmt.Errorf("unsupported length for blake2s: %d", size)
}
func sumBlake2s32(data []byte, _ int) ([]byte, error) {
d := blake2s.Sum256(data)
return d[:], nil
}
Expand Down Expand Up @@ -198,12 +195,9 @@ func registerNonStdlibHashFuncs() {

// Blake family of hash functions
// BLAKE2S
for c := uint64(BLAKE2S_MIN); c <= BLAKE2S_MAX; c++ {
size := int(c - BLAKE2S_MIN + 1)
RegisterHashFunc(c, func(buf []byte, _ int) ([]byte, error) {
return sumBlake2s(buf, size)
})
}
//
// We only support 32byte (256 bit)
RegisterHashFunc(BLAKE2S_MIN+31, sumBlake2s32)
// BLAKE2B
for c := uint64(BLAKE2B_MIN); c <= BLAKE2B_MAX; c++ {
size := int(c - BLAKE2B_MIN + 1)
Expand Down
15 changes: 15 additions & 0 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,18 @@ func TestTooLargeLength(t *testing.T) {
t.Fatal("bad error", err)
}
}

func TestBasicSum(t *testing.T) {
for code, name := range Codes {
defaultLen, ok := DefaultLengths[code]
if !ok {
defaultLen = 32
}
_, err := Sum([]byte("test"), code, defaultLen)
switch err {
case ErrSumNotSupported, nil:
default:
t.Errorf("unexpected error for %s: %s", name, err)
}
}
}