2022-08-28 13:21:57 +02:00
|
|
|
// Package hash provides hash utilities for Fs.
|
2018-01-12 17:30:54 +01:00
|
|
|
package hash
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
2021-08-13 21:17:52 +02:00
|
|
|
"crypto/sha256"
|
2021-11-03 00:34:20 +01:00
|
|
|
"encoding/base64"
|
2016-01-11 13:39:33 +01:00
|
|
|
"encoding/hex"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2016-01-11 13:39:33 +01:00
|
|
|
"fmt"
|
|
|
|
"hash"
|
2019-08-06 14:39:29 +02:00
|
|
|
"hash/crc32"
|
2016-01-11 13:39:33 +01:00
|
|
|
"io"
|
|
|
|
"strings"
|
2016-06-12 16:06:02 +02:00
|
|
|
|
2019-06-26 20:39:01 +02:00
|
|
|
"github.com/jzelinskie/whirlpool"
|
2016-01-11 13:39:33 +01:00
|
|
|
)
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// Type indicates a standard hashing algorithm
|
|
|
|
type Type int
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2019-09-23 15:32:36 +02:00
|
|
|
type hashDefinition struct {
|
|
|
|
width int
|
|
|
|
name string
|
2021-05-21 16:32:33 +02:00
|
|
|
alias string
|
2019-09-23 15:32:36 +02:00
|
|
|
newFunc func() hash.Hash
|
|
|
|
hashType Type
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:32:33 +02:00
|
|
|
var (
|
|
|
|
type2hash = map[Type]*hashDefinition{}
|
|
|
|
name2hash = map[string]*hashDefinition{}
|
|
|
|
alias2hash = map[string]*hashDefinition{}
|
|
|
|
supported = []Type{}
|
|
|
|
)
|
2019-09-23 15:32:36 +02:00
|
|
|
|
|
|
|
// RegisterHash adds a new Hash to the list and returns it Type
|
2021-05-21 16:32:33 +02:00
|
|
|
func RegisterHash(name, alias string, width int, newFunc func() hash.Hash) Type {
|
|
|
|
hashType := Type(1 << len(supported))
|
|
|
|
supported = append(supported, hashType)
|
|
|
|
|
2019-09-23 15:32:36 +02:00
|
|
|
definition := &hashDefinition{
|
|
|
|
name: name,
|
2021-05-21 16:32:33 +02:00
|
|
|
alias: alias,
|
2019-09-23 15:32:36 +02:00
|
|
|
width: width,
|
|
|
|
newFunc: newFunc,
|
2021-05-21 16:32:33 +02:00
|
|
|
hashType: hashType,
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:32:33 +02:00
|
|
|
type2hash[hashType] = definition
|
|
|
|
name2hash[name] = definition
|
|
|
|
alias2hash[alias] = definition
|
|
|
|
|
|
|
|
return hashType
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
|
|
|
|
2023-11-23 20:56:13 +01:00
|
|
|
// SupportOnly makes the hash package only support the types passed
|
|
|
|
// in. Used for testing.
|
|
|
|
//
|
|
|
|
// It returns the previously supported types.
|
|
|
|
func SupportOnly(new []Type) (old []Type) {
|
|
|
|
old = supported
|
|
|
|
supported = new
|
|
|
|
return old
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// ErrUnsupported should be returned by filesystem,
|
2016-01-11 13:39:33 +01:00
|
|
|
// if it is requested to deliver an unsupported hash type.
|
2018-01-18 21:27:52 +01:00
|
|
|
var ErrUnsupported = errors.New("hash type not supported")
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2019-09-23 15:32:36 +02:00
|
|
|
var (
|
|
|
|
// None indicates no hashes are supported
|
|
|
|
None Type
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// MD5 indicates MD5 support
|
2019-09-23 15:32:36 +02:00
|
|
|
MD5 Type
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// SHA1 indicates SHA-1 support
|
2019-09-23 15:32:36 +02:00
|
|
|
SHA1 Type
|
2018-04-20 12:42:46 +02:00
|
|
|
|
2019-06-26 20:39:01 +02:00
|
|
|
// Whirlpool indicates Whirlpool support
|
2019-09-23 15:32:36 +02:00
|
|
|
Whirlpool Type
|
2019-06-26 20:39:01 +02:00
|
|
|
|
2019-08-06 14:39:29 +02:00
|
|
|
// CRC32 indicates CRC-32 support
|
2019-09-23 15:32:36 +02:00
|
|
|
CRC32 Type
|
2021-08-13 21:17:52 +02:00
|
|
|
|
|
|
|
// SHA256 indicates SHA-256 support
|
|
|
|
SHA256 Type
|
2016-01-11 13:39:33 +01:00
|
|
|
)
|
|
|
|
|
2019-09-23 15:32:36 +02:00
|
|
|
func init() {
|
2021-05-21 16:32:33 +02:00
|
|
|
MD5 = RegisterHash("md5", "MD5", 32, md5.New)
|
|
|
|
SHA1 = RegisterHash("sha1", "SHA-1", 40, sha1.New)
|
|
|
|
Whirlpool = RegisterHash("whirlpool", "Whirlpool", 128, whirlpool.New)
|
|
|
|
CRC32 = RegisterHash("crc32", "CRC-32", 8, func() hash.Hash { return crc32.NewIEEE() })
|
2021-08-13 21:17:52 +02:00
|
|
|
SHA256 = RegisterHash("sha256", "SHA-256", 64, sha256.New)
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// Supported returns a set of all the supported hashes by
|
2016-01-11 13:39:33 +01:00
|
|
|
// HashStream and MultiHasher.
|
2019-09-23 15:32:36 +02:00
|
|
|
func Supported() Set {
|
2021-05-21 16:32:33 +02:00
|
|
|
return NewHashSet(supported...)
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// Width returns the width in characters for any HashType
|
2021-11-03 00:34:20 +01:00
|
|
|
func Width(hashType Type, base64Encoded bool) int {
|
2021-05-21 16:32:33 +02:00
|
|
|
if hash := type2hash[hashType]; hash != nil {
|
2021-11-03 00:34:20 +01:00
|
|
|
if base64Encoded {
|
|
|
|
return base64.URLEncoding.EncodedLen(hash.width / 2)
|
|
|
|
}
|
2021-05-21 16:32:33 +02:00
|
|
|
return hash.width
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
|
|
|
return 0
|
2016-01-17 14:56:00 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// Stream will calculate hashes of all supported hash types.
|
|
|
|
func Stream(r io.Reader) (map[Type]string, error) {
|
2019-09-23 15:32:36 +02:00
|
|
|
return StreamTypes(r, Supported())
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// StreamTypes will calculate hashes of the requested hash types.
|
|
|
|
func StreamTypes(r io.Reader, set Set) (map[Type]string, error) {
|
2018-01-18 21:27:52 +01:00
|
|
|
hashers, err := fromTypes(set)
|
2016-01-11 13:39:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
_, err = io.Copy(toMultiWriter(hashers), r)
|
2016-01-11 13:39:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
var ret = make(map[Type]string)
|
2016-01-11 13:39:33 +01:00
|
|
|
for k, v := range hashers {
|
|
|
|
ret[k] = hex.EncodeToString(v.Sum(nil))
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of the hash type.
|
|
|
|
// The function will panic if the hash type is unknown.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Type) String() string {
|
2019-09-23 15:32:36 +02:00
|
|
|
if h == None {
|
2021-05-21 16:32:33 +02:00
|
|
|
return "none"
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
2021-05-21 16:32:33 +02:00
|
|
|
if hash := type2hash[h]; hash != nil {
|
|
|
|
return hash.name
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
2021-05-21 16:32:33 +02:00
|
|
|
panic(fmt.Sprintf("internal error: unknown hash type: 0x%x", int(h)))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:32:33 +02:00
|
|
|
// Set a Type from a flag.
|
|
|
|
// Both name and alias are accepted.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h *Type) Set(s string) error {
|
2021-05-21 16:32:33 +02:00
|
|
|
if s == "none" || s == "None" {
|
2018-01-18 21:27:52 +01:00
|
|
|
*h = None
|
2021-05-21 16:32:33 +02:00
|
|
|
return nil
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
2021-05-21 16:32:33 +02:00
|
|
|
if hash := name2hash[strings.ToLower(s)]; hash != nil {
|
|
|
|
*h = hash.hashType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if hash := alias2hash[s]; hash != nil {
|
|
|
|
*h = hash.hashType
|
|
|
|
return nil
|
2019-09-23 15:32:36 +02:00
|
|
|
}
|
2022-06-08 22:54:39 +02:00
|
|
|
return fmt.Errorf("unknown hash type %q", s)
|
2018-01-06 18:53:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Type) Type() string {
|
2018-01-06 18:53:37 +01:00
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// fromTypes will return hashers for all the requested types.
|
2016-01-11 13:39:33 +01:00
|
|
|
// The types must be a subset of SupportedHashes,
|
|
|
|
// and this function must support all types.
|
2018-01-18 21:27:52 +01:00
|
|
|
func fromTypes(set Set) (map[Type]hash.Hash, error) {
|
2019-09-23 15:32:36 +02:00
|
|
|
if !set.SubsetOf(Supported()) {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("requested set %08x contains unknown hash types", int(set))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
2021-05-21 16:32:33 +02:00
|
|
|
hashers := map[Type]hash.Hash{}
|
2019-09-23 15:32:36 +02:00
|
|
|
|
2021-05-21 16:32:33 +02:00
|
|
|
for _, t := range set.Array() {
|
|
|
|
hash := type2hash[t]
|
|
|
|
if hash == nil {
|
|
|
|
panic(fmt.Sprintf("internal error: Unsupported hash type %v", t))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
2021-05-21 16:32:33 +02:00
|
|
|
hashers[t] = hash.newFunc()
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
2019-09-23 15:32:36 +02:00
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
return hashers, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
// toMultiWriter will return a set of hashers into a
|
2016-01-11 13:39:33 +01:00
|
|
|
// single multiwriter, where one write will update all
|
|
|
|
// the hashers.
|
2018-01-18 21:27:52 +01:00
|
|
|
func toMultiWriter(h map[Type]hash.Hash) io.Writer {
|
2016-01-11 13:39:33 +01:00
|
|
|
// Convert to to slice
|
|
|
|
var w = make([]io.Writer, 0, len(h))
|
|
|
|
for _, v := range h {
|
|
|
|
w = append(w, v)
|
|
|
|
}
|
|
|
|
return io.MultiWriter(w...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A MultiHasher will construct various hashes on
|
|
|
|
// all incoming writes.
|
|
|
|
type MultiHasher struct {
|
2016-06-19 14:49:34 +02:00
|
|
|
w io.Writer
|
|
|
|
size int64
|
2018-01-12 17:30:54 +01:00
|
|
|
h map[Type]hash.Hash // Hashes
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultiHasher will return a hash writer that will write all
|
|
|
|
// supported hash types.
|
|
|
|
func NewMultiHasher() *MultiHasher {
|
2019-09-23 15:32:36 +02:00
|
|
|
h, err := NewMultiHasherTypes(Supported())
|
2016-01-11 13:39:33 +01:00
|
|
|
if err != nil {
|
|
|
|
panic("internal error: could not create multihasher")
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultiHasherTypes will return a hash writer that will write
|
|
|
|
// the requested hash types.
|
2018-01-12 17:30:54 +01:00
|
|
|
func NewMultiHasherTypes(set Set) (*MultiHasher, error) {
|
2018-01-18 21:27:52 +01:00
|
|
|
hashers, err := fromTypes(set)
|
2016-01-11 13:39:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-18 21:27:52 +01:00
|
|
|
m := MultiHasher{h: hashers, w: toMultiWriter(hashers)}
|
2016-01-11 13:39:33 +01:00
|
|
|
return &m, nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 14:49:34 +02:00
|
|
|
func (m *MultiHasher) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = m.w.Write(p)
|
|
|
|
m.size += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Sums returns the sums of all accumulated hashes as hex encoded
|
|
|
|
// strings.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (m *MultiHasher) Sums() map[Type]string {
|
|
|
|
dst := make(map[Type]string)
|
2016-01-11 13:39:33 +01:00
|
|
|
for k, v := range m.h {
|
|
|
|
dst[k] = hex.EncodeToString(v.Sum(nil))
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-09-14 23:02:13 +02:00
|
|
|
// Sum returns the specified hash from the multihasher
|
|
|
|
func (m *MultiHasher) Sum(hashType Type) ([]byte, error) {
|
|
|
|
h, ok := m.h[hashType]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrUnsupported
|
|
|
|
}
|
|
|
|
return h.Sum(nil), nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 00:34:20 +01:00
|
|
|
// SumString returns the specified hash from the multihasher as a hex or base64 encoded string
|
|
|
|
func (m *MultiHasher) SumString(hashType Type, base64Encoded bool) (string, error) {
|
|
|
|
sum, err := m.Sum(hashType)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if base64Encoded {
|
|
|
|
return base64.URLEncoding.EncodeToString(sum), nil
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(sum), nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 14:49:34 +02:00
|
|
|
// Size returns the number of bytes written
|
|
|
|
func (m *MultiHasher) Size() int64 {
|
|
|
|
return m.size
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// A Set Indicates one or more hash types.
|
|
|
|
type Set int
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
// NewHashSet will create a new hash set with the hash types supplied
|
2018-01-12 17:30:54 +01:00
|
|
|
func NewHashSet(t ...Type) Set {
|
2018-01-18 21:27:52 +01:00
|
|
|
h := Set(None)
|
2016-01-11 13:39:33 +01:00
|
|
|
return h.Add(t...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add one or more hash types to the set.
|
|
|
|
// Returns the modified hash set.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h *Set) Add(t ...Type) Set {
|
2016-01-11 13:39:33 +01:00
|
|
|
for _, v := range t {
|
2018-01-12 17:30:54 +01:00
|
|
|
*h |= Set(v)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
return *h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns true if the
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) Contains(t Type) bool {
|
2016-01-11 13:39:33 +01:00
|
|
|
return int(h)&int(t) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overlap returns the overlapping hash types
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) Overlap(t Set) Set {
|
|
|
|
return Set(int(h) & int(t))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubsetOf will return true if all types of h
|
|
|
|
// is present in the set c
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) SubsetOf(c Set) bool {
|
2016-01-11 13:39:33 +01:00
|
|
|
return int(h)|int(c) == int(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOne will return a hash type.
|
|
|
|
// Currently the first is returned, but it could be
|
|
|
|
// improved to return the strongest.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) GetOne() Type {
|
2016-01-11 13:39:33 +01:00
|
|
|
v := int(h)
|
|
|
|
i := uint(0)
|
|
|
|
for v != 0 {
|
|
|
|
if v&1 != 0 {
|
2018-01-12 17:30:54 +01:00
|
|
|
return Type(1 << i)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
v >>= 1
|
|
|
|
}
|
2019-01-11 18:17:46 +01:00
|
|
|
return None
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Array returns an array of all hash types in the set
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) Array() (ht []Type) {
|
2016-01-11 13:39:33 +01:00
|
|
|
v := int(h)
|
|
|
|
i := uint(0)
|
|
|
|
for v != 0 {
|
|
|
|
if v&1 != 0 {
|
2018-01-12 17:30:54 +01:00
|
|
|
ht = append(ht, Type(1<<i))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
v >>= 1
|
|
|
|
}
|
|
|
|
return ht
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of hash types in the set
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) Count() int {
|
2016-01-11 13:39:33 +01:00
|
|
|
if int(h) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// credit: https://code.google.com/u/arnehormann/
|
|
|
|
x := uint64(h)
|
|
|
|
x -= (x >> 1) & 0x5555555555555555
|
|
|
|
x = (x>>2)&0x3333333333333333 + x&0x3333333333333333
|
|
|
|
x += x >> 4
|
|
|
|
x &= 0x0f0f0f0f0f0f0f0f
|
|
|
|
x *= 0x0101010101010101
|
|
|
|
return int(x >> 56)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of the hash set.
|
|
|
|
// The function will panic if it contains an unknown type.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (h Set) String() string {
|
2016-01-11 13:39:33 +01:00
|
|
|
a := h.Array()
|
|
|
|
var r []string
|
|
|
|
for _, v := range a {
|
|
|
|
r = append(r, v.String())
|
|
|
|
}
|
|
|
|
return "[" + strings.Join(r, ", ") + "]"
|
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
|
|
|
|
// Equals checks to see if src == dst, but ignores empty strings
|
|
|
|
// and returns true if either is empty.
|
|
|
|
func Equals(src, dst string) bool {
|
|
|
|
if src == "" || dst == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return src == dst
|
|
|
|
}
|
2021-07-07 17:34:16 +02:00
|
|
|
|
|
|
|
// HelpString returns help message with supported hashes
|
|
|
|
func HelpString(indent int) string {
|
|
|
|
padding := strings.Repeat(" ", indent)
|
|
|
|
var help strings.Builder
|
|
|
|
help.WriteString(padding)
|
|
|
|
help.WriteString("Supported hashes are:\n")
|
|
|
|
for _, h := range supported {
|
|
|
|
fmt.Fprintf(&help, "%s * %v\n", padding, h.String())
|
|
|
|
}
|
|
|
|
return help.String()
|
|
|
|
}
|