-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasher.go
50 lines (40 loc) · 1.16 KB
/
hasher.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
package fastmerkle
import (
"golang.org/x/crypto/sha3"
"hash"
"sync"
)
// fastHasher is a hashing instance shared by multiple processes
type fastHasher struct {
hashEngine hash.Hash // The hash engine that will be used (for now it's Keccak256)
}
// addToHash adds the input data to the hashing engine
func (fh *fastHasher) addToHash(inputData []byte) error {
_, writeErr := fh.hashEngine.Write(inputData)
return writeErr
}
// getHash returns the hash of the input data
func (fh *fastHasher) getHash() []byte {
return fh.hashEngine.Sum(nil)
}
// fastHasherPool is a sync pool that can dish out
// fast hasher instances
var fastHasherPool = sync.Pool{
New: func() interface{} {
return &fastHasher{
hashEngine: sha3.NewLegacyKeccak256(),
}
},
}
// acquireFastHasher acquires a new instance of the hasher
// from the hasher pool. Must be coupled with an adequate release call
func acquireFastHasher() *fastHasher {
fh, _ := fastHasherPool.Get().(*fastHasher)
return fh
}
// releaseFastHasher resets a hasher instance and puts it back
// into the hasher pool for later usage
func releaseFastHasher(fh *fastHasher) {
fh.hashEngine.Reset()
fastHasherPool.Put(fh)
}