From 10afd220b75c0f4bde1a524c2dbf2ab39b5876b4 Mon Sep 17 00:00:00 2001 From: Eric Myhre Date: Sat, 6 Mar 2021 13:42:11 +0100 Subject: [PATCH] Drop DefaultLengths; can now get from Hash.Size. Discussed in https://github.com/multiformats/go-multihash/pull/136#discussion_r588878137 --- multihash.go | 24 ------------------------ sum.go | 7 +------ sum_test.go | 6 +----- 3 files changed, 2 insertions(+), 35 deletions(-) diff --git a/multihash.go b/multihash.go index 35b3610..d70c0df 100644 --- a/multihash.go +++ b/multihash.go @@ -80,7 +80,6 @@ func init() { name := fmt.Sprintf("blake2b-%d", n*8) Names[name] = c Codes[c] = name - DefaultLengths[c] = int(n) } // Add blake2s (32 codes) @@ -89,7 +88,6 @@ func init() { name := fmt.Sprintf("blake2s-%d", n*8) Names[name] = c Codes[c] = name - DefaultLengths[c] = int(n) } } @@ -142,28 +140,6 @@ var Codes = map[uint64]string{ MD5: "md5", } -// DefaultLengths maps a hash code to it's default length -var DefaultLengths = map[uint64]int{ - IDENTITY: -1, - SHA1: 20, - SHA2_256: 32, - SHA2_512: 64, - SHA3_224: 28, - SHA3_256: 32, - SHA3_384: 48, - SHA3_512: 64, - DBL_SHA2_256: 32, - KECCAK_224: 28, - KECCAK_256: 32, - MURMUR3_128: 4, - KECCAK_384: 48, - KECCAK_512: 64, - SHAKE_128: 32, - SHAKE_256: 64, - X11: 64, - MD5: 16, -} - func uvarint(buf []byte) (uint64, []byte, error) { n, c, err := varint.FromUvarint(buf) if err != nil { diff --git a/sum.go b/sum.go index 131c532..b6cdb87 100644 --- a/sum.go +++ b/sum.go @@ -2,7 +2,6 @@ package multihash import ( "errors" - "fmt" ) // ErrSumNotSupported is returned when the Sum function code is not implemented @@ -30,11 +29,7 @@ func Sum(data []byte, code uint64, length int) (Multihash, error) { // Deal with any truncation. if length < 0 { - var ok bool - length, ok = DefaultLengths[code] - if !ok { - return nil, fmt.Errorf("no default length for code %d", code) - } + length = hasher.Size() } if len(sum) < length { return nil, ErrLenTooLarge diff --git a/sum_test.go b/sum_test.go index 3402431..0ad3b75 100644 --- a/sum_test.go +++ b/sum_test.go @@ -131,11 +131,7 @@ func TestTooLargeLength(t *testing.T) { func TestBasicSum(t *testing.T) { for code, name := range multihash.Codes { - defaultLen, ok := multihash.DefaultLengths[code] - if !ok { - defaultLen = 32 - } - _, err := multihash.Sum([]byte("test"), code, defaultLen) + _, err := multihash.Sum([]byte("test"), code, -1) switch err { case multihash.ErrSumNotSupported, nil: default: