-
Notifications
You must be signed in to change notification settings - Fork 502
/
keccak.go
79 lines (62 loc) · 1.52 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
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
package keccak
import (
"hash"
"github.com/umbracle/fastrlp"
"golang.org/x/crypto/sha3"
)
type hashImpl interface {
hash.Hash
Read(b []byte) (int, error)
}
// Keccak is the sha256 keccak hash
type Keccak struct {
buf []byte // buffer to store intermediate rlp marshal values
tmp []byte
hash hashImpl
}
// WriteRlp writes an RLP value
func (k *Keccak) WriteRlp(dst []byte, v *fastrlp.Value) []byte {
k.buf = v.MarshalTo(k.buf[:0])
k.Write(k.buf) //nolint:errcheck
return k.Sum(dst)
}
// WriteFn writes using value provided by custom marshalFn func
func (k *Keccak) WriteFn(dst []byte, marshalFn func([]byte) []byte) []byte {
k.buf = marshalFn(k.buf[:0])
k.Write(k.buf) //nolint:errcheck
return k.Sum(dst)
}
// Write implements the hash interface
func (k *Keccak) Write(b []byte) (int, error) {
return k.hash.Write(b)
}
// Reset implements the hash interface
func (k *Keccak) Reset() {
k.buf = k.buf[:0]
k.hash.Reset()
}
// Read hashes the content and returns the intermediate buffer.
func (k *Keccak) Read() []byte {
k.hash.Read(k.tmp) //nolint:errcheck
return k.tmp
}
// Sum implements the hash interface
func (k *Keccak) Sum(dst []byte) []byte {
k.hash.Read(k.tmp) //nolint:errcheck
dst = append(dst, k.tmp[:]...)
return dst
}
func newKeccak(hash hashImpl) *Keccak {
return &Keccak{
hash: hash,
tmp: make([]byte, hash.Size()),
}
}
// NewKeccak256 returns a new keccak 256
func NewKeccak256() *Keccak {
impl, ok := sha3.NewLegacyKeccak256().(hashImpl)
if !ok {
return nil
}
return newKeccak(impl)
}