-
Notifications
You must be signed in to change notification settings - Fork 199
/
keccak.go
42 lines (34 loc) · 994 Bytes
/
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
package keccak
import (
"github.com/ElrondNetwork/elrond-go/hashing"
"golang.org/x/crypto/sha3"
)
var _ hashing.Hasher = (*Keccak)(nil)
var keccakEmptyHash []byte
// Keccak is a sha3-Keccak implementation of the hasher interface.
type Keccak struct {
}
// Compute takes a string, and returns the sha3-Keccak hash of that string
func (k Keccak) 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 Keccak) 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 (Keccak) Size() int {
return sha3.NewLegacyKeccak256().Size()
}
// IsInterfaceNil returns true if there is no value under the interface
func (k Keccak) IsInterfaceNil() bool {
return false
}