forked from umbracle/fastrlp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keccak.go
54 lines (44 loc) · 985 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
43
44
45
46
47
48
49
50
51
52
53
54
package fastrlp
import (
"hash"
"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
}
// 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)
return k.tmp
}
// Sum implements the hash interface
func (k *Keccak) Sum(dst []byte) []byte {
k.hash.Read(k.tmp)
dst = append(dst, k.tmp[:]...)
return dst
}
func newKeccak(hash hashImpl) *Keccak {
return &Keccak{
hash: hash,
tmp: make([]byte, hash.Size()),
}
}
func NewKeccak256() *Keccak {
return newKeccak(sha3.NewLegacyKeccak256().(hashImpl))
}