forked from TrueCloudLab/rclone
hash: add CRC-32 support
This commit is contained in:
parent
6f16588123
commit
8159658e67
2 changed files with 14 additions and 1 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash"
|
"hash"
|
||||||
|
"hash/crc32"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -40,13 +41,16 @@ const (
|
||||||
// Whirlpool indicates Whirlpool support
|
// Whirlpool indicates Whirlpool support
|
||||||
Whirlpool
|
Whirlpool
|
||||||
|
|
||||||
|
// CRC32 indicates CRC-32 support
|
||||||
|
CRC32
|
||||||
|
|
||||||
// None indicates no hashes are supported
|
// None indicates no hashes are supported
|
||||||
None Type = 0
|
None Type = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
// Supported returns a set of all the supported hashes by
|
// Supported returns a set of all the supported hashes by
|
||||||
// HashStream and MultiHasher.
|
// 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
|
// Width returns the width in characters for any HashType
|
||||||
var Width = map[Type]int{
|
var Width = map[Type]int{
|
||||||
|
@ -55,6 +59,7 @@ var Width = map[Type]int{
|
||||||
Dropbox: 64,
|
Dropbox: 64,
|
||||||
QuickXorHash: 40,
|
QuickXorHash: 40,
|
||||||
Whirlpool: 128,
|
Whirlpool: 128,
|
||||||
|
CRC32: 8,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream will calculate hashes of all supported hash types.
|
// Stream will calculate hashes of all supported hash types.
|
||||||
|
@ -96,6 +101,8 @@ func (h Type) String() string {
|
||||||
return "QuickXorHash"
|
return "QuickXorHash"
|
||||||
case Whirlpool:
|
case Whirlpool:
|
||||||
return "Whirlpool"
|
return "Whirlpool"
|
||||||
|
case CRC32:
|
||||||
|
return "CRC-32"
|
||||||
default:
|
default:
|
||||||
err := fmt.Sprintf("internal error: unknown hash type: 0x%x", int(h))
|
err := fmt.Sprintf("internal error: unknown hash type: 0x%x", int(h))
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -117,6 +124,8 @@ func (h *Type) Set(s string) error {
|
||||||
*h = QuickXorHash
|
*h = QuickXorHash
|
||||||
case "Whirlpool":
|
case "Whirlpool":
|
||||||
*h = Whirlpool
|
*h = Whirlpool
|
||||||
|
case "CRC-32":
|
||||||
|
*h = CRC32
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("Unknown hash type %q", s)
|
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()
|
hashers[t] = quickxorhash.New()
|
||||||
case Whirlpool:
|
case Whirlpool:
|
||||||
hashers[t] = whirlpool.New()
|
hashers[t] = whirlpool.New()
|
||||||
|
case CRC32:
|
||||||
|
hashers[t] = crc32.NewIEEE()
|
||||||
default:
|
default:
|
||||||
err := fmt.Sprintf("internal error: Unsupported hash type %v", t)
|
err := fmt.Sprintf("internal error: Unsupported hash type %v", t)
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -74,6 +74,7 @@ var hashTestSet = []hashTest{
|
||||||
hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
|
hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
|
||||||
hash.QuickXorHash: "0110c000085000031c0001095ec00218d0000700",
|
hash.QuickXorHash: "0110c000085000031c0001095ec00218d0000700",
|
||||||
hash.Whirlpool: "eddf52133d4566d763f716e853d6e4efbabd29e2c2e63f56747b1596172851d34c2df9944beb6640dbdbe3d9b4eb61180720a79e3d15baff31c91e43d63869a4",
|
hash.Whirlpool: "eddf52133d4566d763f716e853d6e4efbabd29e2c2e63f56747b1596172851d34c2df9944beb6640dbdbe3d9b4eb61180720a79e3d15baff31c91e43d63869a4",
|
||||||
|
hash.CRC32: "a6041d7e",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Empty data set
|
// Empty data set
|
||||||
|
@ -85,6 +86,7 @@ var hashTestSet = []hashTest{
|
||||||
hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
||||||
hash.QuickXorHash: "0000000000000000000000000000000000000000",
|
hash.QuickXorHash: "0000000000000000000000000000000000000000",
|
||||||
hash.Whirlpool: "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3",
|
hash.Whirlpool: "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3",
|
||||||
|
hash.CRC32: "00000000",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue