From 4c753498b2e66007218f82356103bf9c48cc2153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Tue, 18 Nov 2025 11:11:19 +0100 Subject: [PATCH] crypto: refactor deduplicate hashing logic --- crypto/keccak.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/crypto/keccak.go b/crypto/keccak.go index 0ad79a63c1..589d5b4969 100644 --- a/crypto/keccak.go +++ b/crypto/keccak.go @@ -39,25 +39,23 @@ var hasherPool = sync.Pool{ // Keccak256 calculates and returns the Keccak256 hash of the input data. func Keccak256(data ...[]byte) []byte { b := make([]byte, 32) - d := hasherPool.Get().(KeccakState) - d.Reset() - for _, b := range data { - d.Write(b) - } - d.Read(b) - hasherPool.Put(d) + hashInto(b, data...) return b } // Keccak256Hash calculates and returns the Keccak256 hash of the input data, // converting it to an internal Hash data structure. func Keccak256Hash(data ...[]byte) (h common.Hash) { + hashInto(h[:], data...) + return h +} + +func hashInto(out []byte, data ...[]byte) { d := hasherPool.Get().(KeccakState) + defer hasherPool.Put(d) d.Reset() for _, b := range data { d.Write(b) } - d.Read(h[:]) - hasherPool.Put(d) - return h + d.Read(out) }