Skip to content

Commit 6f1ea18

Browse files
authored
Merge pull request #129 from multiformats/test/basic-sum-test
fix: only register one blake2s length
2 parents 62f4ba0 + ccdaf55 commit 6f1ea18

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

sum.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) {
6565
return Encode(d, code)
6666
}
6767

68-
func sumBlake2s(data []byte, size int) ([]byte, error) {
69-
if size != 32 {
70-
return nil, fmt.Errorf("unsupported length for blake2s: %d", size)
71-
}
68+
func sumBlake2s32(data []byte, _ int) ([]byte, error) {
7269
d := blake2s.Sum256(data)
7370
return d[:], nil
7471
}
@@ -209,12 +206,9 @@ func registerNonStdlibHashFuncs() {
209206

210207
// Blake family of hash functions
211208
// BLAKE2S
212-
for c := uint64(BLAKE2S_MIN); c <= BLAKE2S_MAX; c++ {
213-
size := int(c - BLAKE2S_MIN + 1)
214-
RegisterHashFunc(c, func(buf []byte, _ int) ([]byte, error) {
215-
return sumBlake2s(buf, size)
216-
})
217-
}
209+
//
210+
// We only support 32byte (256 bit)
211+
RegisterHashFunc(BLAKE2S_MIN+31, sumBlake2s32)
218212
// BLAKE2B
219213
for c := uint64(BLAKE2B_MIN); c <= BLAKE2B_MAX; c++ {
220214
size := int(c - BLAKE2B_MIN + 1)

sum_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,18 @@ func TestTooLargeLength(t *testing.T) {
181181
t.Fatal("bad error", err)
182182
}
183183
}
184+
185+
func TestBasicSum(t *testing.T) {
186+
for code, name := range Codes {
187+
defaultLen, ok := DefaultLengths[code]
188+
if !ok {
189+
defaultLen = 32
190+
}
191+
_, err := Sum([]byte("test"), code, defaultLen)
192+
switch err {
193+
case ErrSumNotSupported, nil:
194+
default:
195+
t.Errorf("unexpected error for %s: %s", name, err)
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)