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