github.com/lmittmann/w3@v0.20.0/internal/crypto/keccak.go (about) 1 package crypto 2 3 import ( 4 "sync" 5 6 "github.com/ethereum/go-ethereum/common" 7 "github.com/ethereum/go-ethereum/crypto" 8 ) 9 10 var keccakStatePool = sync.Pool{ 11 New: func() any { 12 return crypto.NewKeccakState() 13 }, 14 } 15 16 // Keccak256Hash returns the Keccak256 hash of the input data as [common.Hash]. 17 // 18 // Its implementation is similar to [crypto.Keccak256Hash], but it reuses the 19 // [crypto.KeccakState] to reduce the number of allocations. 20 func Keccak256Hash(data ...[]byte) (hash common.Hash) { 21 // get Keccak state from pool 22 d := keccakStatePool.Get().(crypto.KeccakState) 23 24 for _, b := range data { 25 d.Write(b) 26 } 27 d.Read(hash[:]) 28 29 // reset state and put it back into the pool 30 d.Reset() 31 keccakStatePool.Put(d) 32 33 return hash 34 } 35 36 // Keccak256 returns the Keccak256 hash of the input data. 37 // 38 // Its implementation is similar to [crypto.Keccak256], but it reuses the 39 // [crypto.KeccakState] to reduce the number of allocations. 40 func Keccak256(data ...[]byte) (hash []byte) { 41 return Keccak256Hash(data...).Bytes() 42 }