forked from kalafut/imohash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparsehash.go
113 lines (98 loc) · 2.67 KB
/
sparsehash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Package sparsehash implements a fast, constant-time hash for files. It hashes three fixed
// samples of the file (beginning, middle and end) and uses file size to construct the hash.
//
// For more information, including important caveats on usage, consult https://github.com/oliverpool/sparsehash.
package sparsehash
import (
"bytes"
"hash"
"io"
"os"
)
// Hasher respresents a sparse hasher
type Hasher struct {
// SubHasher is the actual hash function through which the samples will be hashed (murmur3.New128() for instance)
SubHasher func() hash.Hash
// Size of the 3 samples to actually hash (at the beginning, middle and end of the input)
// A SampleSize of 0 will hash alls inputs entirely
SampleSize int64
// Minimum size of the input to only hash the 3 samples (smaller inputs will be hashed entirely)
SizeThreshold int64
}
// New returns a new sparsehash using the specified subhasher as hasher, 16K as sample size
// and 128K as threshhold values.
func New(subhasher func() hash.Hash) Hasher {
return Hasher{
SubHasher: subhasher,
SampleSize: 16 * 1024,
SizeThreshold: 128 * 1024,
}
}
// SumBytes hashes a byte slice using the sparsehash parameters.
func (h Hasher) SumBytes(data []byte) ([]byte, error) {
sr := io.NewSectionReader(bytes.NewReader(data), 0, int64(len(data)))
return h.Sum(sr)
}
// SumFile hashes a file sparsely
func (h Hasher) SumFile(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
sr := io.NewSectionReader(f, 0, fi.Size())
return h.Sum(sr)
}
// Sum hashes a SectionReader using the sparsehash parameters.
func (h Hasher) Sum(f *io.SectionReader) ([]byte, error) {
if f.Size() < h.SizeThreshold || h.SampleSize < 1 {
return h.hashAll(f)
}
return h.hashSamples(f)
}
func (h Hasher) hashAll(f *io.SectionReader) ([]byte, error) {
hasher := h.SubHasher()
_, err := io.Copy(hasher, f)
return hasher.Sum(nil), err
}
func (h Hasher) hashSamples(f *io.SectionReader) ([]byte, error) {
var err error
// The following functions do nothing if err != nil
hasher := h.SubHasher()
hWrite := func(p []byte) {
if err != nil {
return
}
_, err = hasher.Write(p)
}
fRead := func(p []byte) {
if err != nil {
return
}
_, err = f.Read(p)
if err == io.EOF {
err = nil
}
}
fSeek := func(offset int64, whence int) {
if err != nil {
return
}
_, err = f.Seek(offset, whence)
}
// actual work
buffer := make([]byte, h.SampleSize)
fRead(buffer)
hWrite(buffer)
fSeek(f.Size()/2, 0)
fRead(buffer)
hWrite(buffer)
fSeek(-h.SampleSize, 2)
fRead(buffer)
hWrite(buffer)
return hasher.Sum(nil), err
}