2018-01-12 16:30:54 +00:00
|
|
|
package hash
|
2016-01-11 12:39:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2016-06-12 14:06:02 +00:00
|
|
|
|
2019-06-26 18:39:01 +00:00
|
|
|
"github.com/jzelinskie/whirlpool"
|
2016-06-12 14:06:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/backend/dropbox/dbhash"
|
|
|
|
"github.com/rclone/rclone/backend/onedrive/quickxorhash"
|
2016-01-11 12:39:33 +00:00
|
|
|
)
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Type indicates a standard hashing algorithm
|
|
|
|
type Type int
|
2016-01-11 12:39:33 +00:00
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// ErrUnsupported should be returned by filesystem,
|
2016-01-11 12:39:33 +00:00
|
|
|
// if it is requested to deliver an unsupported hash type.
|
2018-01-18 20:27:52 +00:00
|
|
|
var ErrUnsupported = errors.New("hash type not supported")
|
2016-01-11 12:39:33 +00:00
|
|
|
|
|
|
|
const (
|
2018-01-18 20:27:52 +00:00
|
|
|
// MD5 indicates MD5 support
|
|
|
|
MD5 Type = 1 << iota
|
2016-01-11 12:39:33 +00:00
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// SHA1 indicates SHA-1 support
|
|
|
|
SHA1
|
2016-01-17 10:33:40 +00:00
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// Dropbox indicates Dropbox special hash
|
2017-05-26 14:09:31 +00:00
|
|
|
// https://www.dropbox.com/developers/reference/content-hash
|
2018-01-18 20:27:52 +00:00
|
|
|
Dropbox
|
2017-05-26 14:09:31 +00:00
|
|
|
|
2018-04-20 10:42:46 +00:00
|
|
|
// QuickXorHash indicates Microsoft onedrive hash
|
|
|
|
// https://docs.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash
|
|
|
|
QuickXorHash
|
|
|
|
|
2019-06-26 18:39:01 +00:00
|
|
|
// Whirlpool indicates Whirlpool support
|
|
|
|
Whirlpool
|
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// None indicates no hashes are supported
|
|
|
|
None Type = 0
|
2016-01-11 12:39:33 +00:00
|
|
|
)
|
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// Supported returns a set of all the supported hashes by
|
2016-01-11 12:39:33 +00:00
|
|
|
// HashStream and MultiHasher.
|
2019-06-26 18:39:01 +00:00
|
|
|
var Supported = NewHashSet(MD5, SHA1, Dropbox, QuickXorHash, Whirlpool)
|
2016-01-11 12:39:33 +00:00
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// Width returns the width in characters for any HashType
|
|
|
|
var Width = map[Type]int{
|
2018-04-20 10:42:46 +00:00
|
|
|
MD5: 32,
|
|
|
|
SHA1: 40,
|
|
|
|
Dropbox: 64,
|
|
|
|
QuickXorHash: 40,
|
2019-06-26 18:39:01 +00:00
|
|
|
Whirlpool: 128,
|
2016-01-17 13:56:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Stream will calculate hashes of all supported hash types.
|
|
|
|
func Stream(r io.Reader) (map[Type]string, error) {
|
2018-01-18 20:27:52 +00:00
|
|
|
return StreamTypes(r, Supported)
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// StreamTypes will calculate hashes of the requested hash types.
|
|
|
|
func StreamTypes(r io.Reader, set Set) (map[Type]string, error) {
|
2018-01-18 20:27:52 +00:00
|
|
|
hashers, err := fromTypes(set)
|
2016-01-11 12:39:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
_, err = io.Copy(toMultiWriter(hashers), r)
|
2016-01-11 12:39:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
var ret = make(map[Type]string)
|
2016-01-11 12:39:33 +00: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 16:30:54 +00:00
|
|
|
func (h Type) String() string {
|
2016-01-11 12:39:33 +00:00
|
|
|
switch h {
|
2018-01-18 20:27:52 +00:00
|
|
|
case None:
|
2016-01-11 12:39:33 +00:00
|
|
|
return "None"
|
2018-01-18 20:27:52 +00:00
|
|
|
case MD5:
|
2016-01-11 12:39:33 +00:00
|
|
|
return "MD5"
|
2018-01-18 20:27:52 +00:00
|
|
|
case SHA1:
|
2016-01-11 12:39:33 +00:00
|
|
|
return "SHA-1"
|
2018-01-18 20:27:52 +00:00
|
|
|
case Dropbox:
|
2017-05-26 14:09:31 +00:00
|
|
|
return "DropboxHash"
|
2018-04-20 10:42:46 +00:00
|
|
|
case QuickXorHash:
|
|
|
|
return "QuickXorHash"
|
2019-06-26 18:39:01 +00:00
|
|
|
case Whirlpool:
|
|
|
|
return "Whirlpool"
|
2016-01-11 12:39:33 +00:00
|
|
|
default:
|
|
|
|
err := fmt.Sprintf("internal error: unknown hash type: 0x%x", int(h))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// Set a Type from a flag
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h *Type) Set(s string) error {
|
2018-01-06 17:53:37 +00:00
|
|
|
switch s {
|
|
|
|
case "None":
|
2018-01-18 20:27:52 +00:00
|
|
|
*h = None
|
2018-01-06 17:53:37 +00:00
|
|
|
case "MD5":
|
2018-01-18 20:27:52 +00:00
|
|
|
*h = MD5
|
2018-01-06 17:53:37 +00:00
|
|
|
case "SHA-1":
|
2018-01-18 20:27:52 +00:00
|
|
|
*h = SHA1
|
2018-01-06 17:53:37 +00:00
|
|
|
case "DropboxHash":
|
2018-01-18 20:27:52 +00:00
|
|
|
*h = Dropbox
|
2018-04-20 10:42:46 +00:00
|
|
|
case "QuickXorHash":
|
|
|
|
*h = QuickXorHash
|
2019-06-26 18:39:01 +00:00
|
|
|
case "Whirlpool":
|
|
|
|
*h = Whirlpool
|
2018-01-06 17:53:37 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("Unknown hash type %q", s)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h Type) Type() string {
|
2018-01-06 17:53:37 +00:00
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// fromTypes will return hashers for all the requested types.
|
2016-01-11 12:39:33 +00:00
|
|
|
// The types must be a subset of SupportedHashes,
|
|
|
|
// and this function must support all types.
|
2018-01-18 20:27:52 +00:00
|
|
|
func fromTypes(set Set) (map[Type]hash.Hash, error) {
|
|
|
|
if !set.SubsetOf(Supported) {
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.Errorf("requested set %08x contains unknown hash types", int(set))
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
var hashers = make(map[Type]hash.Hash)
|
2016-01-11 12:39:33 +00:00
|
|
|
types := set.Array()
|
|
|
|
for _, t := range types {
|
|
|
|
switch t {
|
2018-01-18 20:27:52 +00:00
|
|
|
case MD5:
|
2016-01-11 12:39:33 +00:00
|
|
|
hashers[t] = md5.New()
|
2018-01-18 20:27:52 +00:00
|
|
|
case SHA1:
|
2016-01-11 12:39:33 +00:00
|
|
|
hashers[t] = sha1.New()
|
2018-01-18 20:27:52 +00:00
|
|
|
case Dropbox:
|
2017-05-26 14:09:31 +00:00
|
|
|
hashers[t] = dbhash.New()
|
2018-04-20 10:42:46 +00:00
|
|
|
case QuickXorHash:
|
|
|
|
hashers[t] = quickxorhash.New()
|
2019-06-26 18:39:01 +00:00
|
|
|
case Whirlpool:
|
|
|
|
hashers[t] = whirlpool.New()
|
2016-01-11 12:39:33 +00:00
|
|
|
default:
|
|
|
|
err := fmt.Sprintf("internal error: Unsupported hash type %v", t)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hashers, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 20:27:52 +00:00
|
|
|
// toMultiWriter will return a set of hashers into a
|
2016-01-11 12:39:33 +00:00
|
|
|
// single multiwriter, where one write will update all
|
|
|
|
// the hashers.
|
2018-01-18 20:27:52 +00:00
|
|
|
func toMultiWriter(h map[Type]hash.Hash) io.Writer {
|
2016-01-11 12:39:33 +00: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 12:49:34 +00:00
|
|
|
w io.Writer
|
|
|
|
size int64
|
2018-01-12 16:30:54 +00:00
|
|
|
h map[Type]hash.Hash // Hashes
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMultiHasher will return a hash writer that will write all
|
|
|
|
// supported hash types.
|
|
|
|
func NewMultiHasher() *MultiHasher {
|
2018-01-18 20:27:52 +00:00
|
|
|
h, err := NewMultiHasherTypes(Supported)
|
2016-01-11 12:39:33 +00: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 16:30:54 +00:00
|
|
|
func NewMultiHasherTypes(set Set) (*MultiHasher, error) {
|
2018-01-18 20:27:52 +00:00
|
|
|
hashers, err := fromTypes(set)
|
2016-01-11 12:39:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-18 20:27:52 +00:00
|
|
|
m := MultiHasher{h: hashers, w: toMultiWriter(hashers)}
|
2016-01-11 12:39:33 +00:00
|
|
|
return &m, nil
|
|
|
|
}
|
|
|
|
|
2016-06-19 12:49:34 +00: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 12:39:33 +00:00
|
|
|
// Sums returns the sums of all accumulated hashes as hex encoded
|
|
|
|
// strings.
|
2018-01-12 16:30:54 +00:00
|
|
|
func (m *MultiHasher) Sums() map[Type]string {
|
|
|
|
dst := make(map[Type]string)
|
2016-01-11 12:39:33 +00:00
|
|
|
for k, v := range m.h {
|
|
|
|
dst[k] = hex.EncodeToString(v.Sum(nil))
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2016-06-19 12:49:34 +00:00
|
|
|
// Size returns the number of bytes written
|
|
|
|
func (m *MultiHasher) Size() int64 {
|
|
|
|
return m.size
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// A Set Indicates one or more hash types.
|
|
|
|
type Set int
|
2016-01-11 12:39:33 +00:00
|
|
|
|
|
|
|
// NewHashSet will create a new hash set with the hash types supplied
|
2018-01-12 16:30:54 +00:00
|
|
|
func NewHashSet(t ...Type) Set {
|
2018-01-18 20:27:52 +00:00
|
|
|
h := Set(None)
|
2016-01-11 12:39:33 +00:00
|
|
|
return h.Add(t...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add one or more hash types to the set.
|
|
|
|
// Returns the modified hash set.
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h *Set) Add(t ...Type) Set {
|
2016-01-11 12:39:33 +00:00
|
|
|
for _, v := range t {
|
2018-01-12 16:30:54 +00:00
|
|
|
*h |= Set(v)
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
return *h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns true if the
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h Set) Contains(t Type) bool {
|
2016-01-11 12:39:33 +00:00
|
|
|
return int(h)&int(t) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overlap returns the overlapping hash types
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h Set) Overlap(t Set) Set {
|
|
|
|
return Set(int(h) & int(t))
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubsetOf will return true if all types of h
|
|
|
|
// is present in the set c
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h Set) SubsetOf(c Set) bool {
|
2016-01-11 12:39:33 +00: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 16:30:54 +00:00
|
|
|
func (h Set) GetOne() Type {
|
2016-01-11 12:39:33 +00:00
|
|
|
v := int(h)
|
|
|
|
i := uint(0)
|
|
|
|
for v != 0 {
|
|
|
|
if v&1 != 0 {
|
2018-01-12 16:30:54 +00:00
|
|
|
return Type(1 << i)
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
v >>= 1
|
|
|
|
}
|
2019-01-11 17:17:46 +00:00
|
|
|
return None
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Array returns an array of all hash types in the set
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h Set) Array() (ht []Type) {
|
2016-01-11 12:39:33 +00:00
|
|
|
v := int(h)
|
|
|
|
i := uint(0)
|
|
|
|
for v != 0 {
|
|
|
|
if v&1 != 0 {
|
2018-01-12 16:30:54 +00:00
|
|
|
ht = append(ht, Type(1<<i))
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
v >>= 1
|
|
|
|
}
|
|
|
|
return ht
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the number of hash types in the set
|
2018-01-12 16:30:54 +00:00
|
|
|
func (h Set) Count() int {
|
2016-01-11 12:39:33 +00: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 16:30:54 +00:00
|
|
|
func (h Set) String() string {
|
2016-01-11 12:39:33 +00:00
|
|
|
a := h.Array()
|
|
|
|
var r []string
|
|
|
|
for _, v := range a {
|
|
|
|
r = append(r, v.String())
|
|
|
|
}
|
|
|
|
return "[" + strings.Join(r, ", ") + "]"
|
|
|
|
}
|
2018-01-12 16:30:54 +00: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
|
|
|
|
}
|