-
Notifications
You must be signed in to change notification settings - Fork 0
/
keccak.go
executable file
·53 lines (43 loc) · 1.17 KB
/
keccak.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
package keccak
import (
"github.com/ThotaGopichandThota/gn-core/hashing"
"golang.org/x/crypto/sha3"
)
var _ hashing.Hasher = (*keccak)(nil)
// keccak is a sha3-keccak implementation of the hasher interface.
type keccak struct {
emptyHash []byte
}
// NewKeccak initializes the empty hash and returns a new instance of the keccak hasher
func NewKeccak() *keccak {
return &keccak{
emptyHash: computeEmptyHash(),
}
}
// Compute takes a string, and returns the sha3-keccak hash of that string
func (k *keccak) Compute(s string) []byte {
if len(s) == 0 {
return k.getEmptyHash()
}
h := sha3.NewLegacyKeccak256()
_, _ = h.Write([]byte(s))
return h.Sum(nil)
}
// Size returns the size, in number of bytes, of a sha3-keccak hash
func (k *keccak) Size() int {
return sha3.NewLegacyKeccak256().Size()
}
func (k *keccak) getEmptyHash() []byte {
hashCopy := make([]byte, len(k.emptyHash))
copy(hashCopy, k.emptyHash)
return hashCopy
}
func computeEmptyHash() []byte {
h := sha3.NewLegacyKeccak256()
_, _ = h.Write([]byte(""))
return h.Sum(nil)
}
// IsInterfaceNil returns true if there is no value under the interface
func (k *keccak) IsInterfaceNil() bool {
return k == nil
}