-
Notifications
You must be signed in to change notification settings - Fork 1
/
workerHash.go
142 lines (130 loc) · 2.76 KB
/
workerHash.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package fsdd
import (
"crypto/sha512"
"hash/maphash"
"io"
"os"
)
//
// WORKER
//
func workerFileHash(worker int) {
switch c.FastHash {
case true:
for i := 0; i < worker; i++ {
go func() {
for target := range namesChan {
hashChan <- &file{name: target, fasthash: fastHash(target)}
}
bg.Done()
}()
}
case false:
for i := 0; i < worker; i++ {
go func() {
for target := range namesChan {
hashChan <- &file{name: target, hash: hash(target)}
}
bg.Done()
}()
}
}
}
func feedFileHash() {
dupes := make(map[uint64]bool) // avoid hashing inode dupes
for _, v := range nfs {
l := len(v)
if l > 1 {
for i := 0; i < l; i++ {
node := v[i]
if _, ok := dupes[node]; ok {
continue
}
dupes[node] = true
namesChan <- xfs[v[i]][0]
}
}
}
close(namesChan)
}
func collectFileHash() {
switch c.FastHash {
case true:
go collectorFastHash()
default:
go collectorHash()
}
}
func collectorFastHash() {
for f := range hashChan {
hFs[f.fasthash] = append(hFs[f.fasthash], f.name)
}
global.Done()
}
func collectorHash() {
for f := range hashChan {
hfs[f.hash] = append(hfs[f.hash], f.name)
}
global.Done()
}
//
// BACKEND
//
const (
_hashSize = 32
_hashBlockSize = 1024 * 32
)
// hash a file via sha512/256
// fast enough on most modern 64bit arm64/x86-64 systems with sha assisted hardware instruction set
func hash(file string) [_hashSize]byte {
f, _ := os.Open(file) // access already verified, skip double check here
r, h := io.Reader(f), sha512.New512_256()
for {
block := make([]byte, _hashBlockSize)
l, _ := r.Read(block)
if l < _hashBlockSize {
_, err := h.Write(block)
if err != nil {
panic("[internal error] [unable to continue] [hash] [state]")
}
break
}
_, err := h.Write(block)
if err != nil {
panic("[internal error] [unable to continue] [hash] [state]")
}
}
f.Close()
var hashOut [_hashSize]byte
for k, v := range h.Sum(nil) {
hashOut[k] = v
}
return hashOut
}
// mseed intit an static seed
var mseed = maphash.MakeSeed()
// fasthash hash a via the new maphash pkg
// extreme fast, but not secure against intentional crafted collisions (exact filesize & filehash must meet here, hard to archive)
func fastHash(file string) uint64 {
f, _ := os.Open(file) // access already verified, skip double check here
r := io.Reader(f)
var h maphash.Hash
h.SetSeed(mseed)
for {
block := make([]byte, _hashBlockSize)
l, _ := r.Read(block)
if l < _hashBlockSize {
_, err := h.Write(block)
if err != nil {
panic("[internal error] [unable to continue] [hash] [state] [maphash]")
}
break
}
_, err := h.Write(block)
if err != nil {
panic("[internal error] [unable to continue] [hash] [state] [maphash]")
}
}
f.Close()
return h.Sum64()
}