forked from TrueCloudLab/rclone
fs/hash: install QuickXorHash as a supported rclone hash type #2262
This commit is contained in:
parent
c51d97c752
commit
bcdb7719c6
2 changed files with 28 additions and 14 deletions
|
@ -10,6 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ncw/rclone/backend/dropbox/dbhash"
|
"github.com/ncw/rclone/backend/dropbox/dbhash"
|
||||||
|
"github.com/ncw/rclone/backend/onedrive/quickxorhash"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,19 +32,24 @@ const (
|
||||||
// https://www.dropbox.com/developers/reference/content-hash
|
// https://www.dropbox.com/developers/reference/content-hash
|
||||||
Dropbox
|
Dropbox
|
||||||
|
|
||||||
|
// QuickXorHash indicates Microsoft onedrive hash
|
||||||
|
// https://docs.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash
|
||||||
|
QuickXorHash
|
||||||
|
|
||||||
// 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)
|
var Supported = NewHashSet(MD5, SHA1, Dropbox, QuickXorHash)
|
||||||
|
|
||||||
// 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{
|
||||||
MD5: 32,
|
MD5: 32,
|
||||||
SHA1: 40,
|
SHA1: 40,
|
||||||
Dropbox: 64,
|
Dropbox: 64,
|
||||||
|
QuickXorHash: 40,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream will calculate hashes of all supported hash types.
|
// Stream will calculate hashes of all supported hash types.
|
||||||
|
@ -81,6 +87,8 @@ func (h Type) String() string {
|
||||||
return "SHA-1"
|
return "SHA-1"
|
||||||
case Dropbox:
|
case Dropbox:
|
||||||
return "DropboxHash"
|
return "DropboxHash"
|
||||||
|
case QuickXorHash:
|
||||||
|
return "QuickXorHash"
|
||||||
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)
|
||||||
|
@ -98,6 +106,8 @@ func (h *Type) Set(s string) error {
|
||||||
*h = SHA1
|
*h = SHA1
|
||||||
case "DropboxHash":
|
case "DropboxHash":
|
||||||
*h = Dropbox
|
*h = Dropbox
|
||||||
|
case "QuickXorHash":
|
||||||
|
*h = QuickXorHash
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("Unknown hash type %q", s)
|
return errors.Errorf("Unknown hash type %q", s)
|
||||||
}
|
}
|
||||||
|
@ -126,6 +136,8 @@ func fromTypes(set Set) (map[Type]hash.Hash, error) {
|
||||||
hashers[t] = sha1.New()
|
hashers[t] = sha1.New()
|
||||||
case Dropbox:
|
case Dropbox:
|
||||||
hashers[t] = dbhash.New()
|
hashers[t] = dbhash.New()
|
||||||
|
case QuickXorHash:
|
||||||
|
hashers[t] = quickxorhash.New()
|
||||||
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)
|
||||||
|
|
|
@ -72,6 +72,7 @@ var hashTestSet = []hashTest{
|
||||||
hash.MD5: "bf13fc19e5151ac57d4252e0e0f87abe",
|
hash.MD5: "bf13fc19e5151ac57d4252e0e0f87abe",
|
||||||
hash.SHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166",
|
hash.SHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166",
|
||||||
hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
|
hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
|
||||||
|
hash.QuickXorHash: "0110c000085000031c0001095ec00218d0000700",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Empty data set
|
// Empty data set
|
||||||
|
@ -81,6 +82,7 @@ var hashTestSet = []hashTest{
|
||||||
hash.MD5: "d41d8cd98f00b204e9800998ecf8427e",
|
hash.MD5: "d41d8cd98f00b204e9800998ecf8427e",
|
||||||
hash.SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
hash.SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
||||||
hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
||||||
|
hash.QuickXorHash: "0000000000000000000000000000000000000000",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -95,13 +97,13 @@ func TestMultiHasher(t *testing.T) {
|
||||||
for k, v := range sums {
|
for k, v := range sums {
|
||||||
expect, ok := test.output[k]
|
expect, ok := test.output[k]
|
||||||
require.True(t, ok, "test output for hash not found")
|
require.True(t, ok, "test output for hash not found")
|
||||||
assert.Equal(t, v, expect)
|
assert.Equal(t, expect, v)
|
||||||
}
|
}
|
||||||
// Test that all are present
|
// Test that all are present
|
||||||
for k, v := range test.output {
|
for k, v := range test.output {
|
||||||
expect, ok := sums[k]
|
expect, ok := sums[k]
|
||||||
require.True(t, ok, "test output for hash not found")
|
require.True(t, ok, "test output for hash not found")
|
||||||
assert.Equal(t, v, expect)
|
assert.Equal(t, expect, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,8 +153,8 @@ func TestHashStreamTypes(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHashSetStringer(t *testing.T) {
|
func TestHashSetStringer(t *testing.T) {
|
||||||
h := hash.NewHashSet(hash.SHA1, hash.MD5, hash.Dropbox)
|
h := hash.NewHashSet(hash.SHA1, hash.MD5, hash.Dropbox, hash.QuickXorHash)
|
||||||
assert.Equal(t, h.String(), "[MD5, SHA-1, DropboxHash]")
|
assert.Equal(t, h.String(), "[MD5, SHA-1, DropboxHash, QuickXorHash]")
|
||||||
h = hash.NewHashSet(hash.SHA1)
|
h = hash.NewHashSet(hash.SHA1)
|
||||||
assert.Equal(t, h.String(), "[SHA-1]")
|
assert.Equal(t, h.String(), "[SHA-1]")
|
||||||
h = hash.NewHashSet()
|
h = hash.NewHashSet()
|
||||||
|
|
Loading…
Reference in a new issue