hash: add CRC-32 support

s3-about
Cenk Alti 2019-08-06 15:39:29 +03:00 committed by Nick Craig-Wood
parent 6f16588123
commit 8159658e67
2 changed files with 14 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/hex"
"fmt"
"hash"
"hash/crc32"
"io"
"strings"
@ -40,13 +41,16 @@ const (
// Whirlpool indicates Whirlpool support
Whirlpool
// CRC32 indicates CRC-32 support
CRC32
// None indicates no hashes are supported
None Type = 0
)
// Supported returns a set of all the supported hashes by
// HashStream and MultiHasher.
var Supported = NewHashSet(MD5, SHA1, Dropbox, QuickXorHash, Whirlpool)
var Supported = NewHashSet(MD5, SHA1, Dropbox, QuickXorHash, Whirlpool, CRC32)
// Width returns the width in characters for any HashType
var Width = map[Type]int{
@ -55,6 +59,7 @@ var Width = map[Type]int{
Dropbox: 64,
QuickXorHash: 40,
Whirlpool: 128,
CRC32: 8,
}
// Stream will calculate hashes of all supported hash types.
@ -96,6 +101,8 @@ func (h Type) String() string {
return "QuickXorHash"
case Whirlpool:
return "Whirlpool"
case CRC32:
return "CRC-32"
default:
err := fmt.Sprintf("internal error: unknown hash type: 0x%x", int(h))
panic(err)
@ -117,6 +124,8 @@ func (h *Type) Set(s string) error {
*h = QuickXorHash
case "Whirlpool":
*h = Whirlpool
case "CRC-32":
*h = CRC32
default:
return errors.Errorf("Unknown hash type %q", s)
}
@ -149,6 +158,8 @@ func fromTypes(set Set) (map[Type]hash.Hash, error) {
hashers[t] = quickxorhash.New()
case Whirlpool:
hashers[t] = whirlpool.New()
case CRC32:
hashers[t] = crc32.NewIEEE()
default:
err := fmt.Sprintf("internal error: Unsupported hash type %v", t)
panic(err)

View File

@ -74,6 +74,7 @@ var hashTestSet = []hashTest{
hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
hash.QuickXorHash: "0110c000085000031c0001095ec00218d0000700",
hash.Whirlpool: "eddf52133d4566d763f716e853d6e4efbabd29e2c2e63f56747b1596172851d34c2df9944beb6640dbdbe3d9b4eb61180720a79e3d15baff31c91e43d63869a4",
hash.CRC32: "a6041d7e",
},
},
// Empty data set
@ -85,6 +86,7 @@ var hashTestSet = []hashTest{
hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
hash.QuickXorHash: "0000000000000000000000000000000000000000",
hash.Whirlpool: "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3",
hash.CRC32: "00000000",
},
},
}