@@ -25,39 +25,38 @@ import (
2525 "golang.org/x/crypto/sha3"
2626)
2727
28- // NewKeccakState creates a new KeccakState
29- func NewKeccakState () KeccakState {
30- return sha3 .NewLegacyKeccak256 ().(KeccakState )
28+ // NewKeccakSponge creates a new KeccakSponge
29+ func NewKeccakSponge () KeccakSponge {
30+ return sha3 .NewLegacyKeccak256 ().(KeccakSponge )
3131}
3232
3333var hasherPool = sync.Pool {
3434 New : func () any {
35- return sha3 .NewLegacyKeccak256 ().(KeccakState )
35+ return sha3 .NewLegacyKeccak256 ().(KeccakSponge )
3636 },
3737}
3838
39- // Keccak256 calculates and returns the Keccak256 hash of the input data.
40- func Keccak256 (data ... []byte ) []byte {
41- b := make ([]byte , 32 )
42- d := hasherPool .Get ().(KeccakState )
39+ // keccak256Sum is a helper that hashes the input data using the provided KeccakSponge and writes the result to out.
40+ func keccak256Sum (out []byte , data ... []byte ) {
41+ d := hasherPool .Get ().(KeccakSponge )
4342 d .Reset ()
4443 for _ , b := range data {
4544 d .Write (b )
4645 }
47- d .Read (b )
46+ d .Read (out )
4847 hasherPool .Put (d )
48+ }
49+
50+ // Keccak256 calculates and returns the Keccak256 hash of the input data.
51+ func Keccak256 (data ... []byte ) []byte {
52+ b := make ([]byte , 32 )
53+ keccak256Sum (b , data ... )
4954 return b
5055}
5156
5257// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
5358// converting it to an internal Hash data structure.
5459func Keccak256Hash (data ... []byte ) (h common.Hash ) {
55- d := hasherPool .Get ().(KeccakState )
56- d .Reset ()
57- for _ , b := range data {
58- d .Write (b )
59- }
60- d .Read (h [:])
61- hasherPool .Put (d )
60+ keccak256Sum (h [:], data ... )
6261 return h
6362}
0 commit comments