/
keccakMock.go
39 lines (32 loc) · 938 Bytes
/
keccakMock.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
package testscommon
import (
"golang.org/x/crypto/sha3"
)
var keccakEmptyHash []byte
// KeccakMock is a sha3-Keccak implementation of the hasher interface.
type KeccakMock struct {
}
// Compute takes a string, and returns the sha3-Keccak hash of that string
func (k KeccakMock) Compute(s string) []byte {
if len(s) == 0 && len(keccakEmptyHash) != 0 {
return k.EmptyHash()
}
h := sha3.NewLegacyKeccak256()
_, _ = h.Write([]byte(s))
return h.Sum(nil)
}
// EmptyHash returns the sha3-Keccak hash of the empty string
func (k KeccakMock) EmptyHash() []byte {
if len(keccakEmptyHash) == 0 {
keccakEmptyHash = k.Compute("")
}
return keccakEmptyHash
}
// Size returns the size, in number of bytes, of a sha3-Keccak hash
func (KeccakMock) Size() int {
return sha3.NewLegacyKeccak256().Size()
}
// IsInterfaceNil returns true if there is no value under the interface
func (k KeccakMock) IsInterfaceNil() bool {
return false
}