Skip to content

Commit

Permalink
Drop DefaultLengths; can now get from Hash.Size.
Browse files Browse the repository at this point in the history
Discussed in
#136 (comment)
  • Loading branch information
warpfork committed Mar 6, 2021
1 parent 57ca955 commit 10afd22
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 35 deletions.
24 changes: 0 additions & 24 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -89,7 +88,6 @@ func init() {
name := fmt.Sprintf("blake2s-%d", n*8)
Names[name] = c
Codes[c] = name
DefaultLengths[c] = int(n)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package multihash

import (
"errors"
"fmt"
)

// ErrSumNotSupported is returned when the Sum function code is not implemented
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 10afd22

Please sign in to comment.