crypto: refactor deduplicate hashing logic

This commit is contained in:
Piotr Mikołajczyk 2025-11-18 11:11:19 +01:00
parent 5e6f7374de
commit 4c753498b2
No known key found for this signature in database
GPG key ID: 2E4C2AAD5E71D22D

View file

@ -39,25 +39,23 @@ var hasherPool = sync.Pool{
// Keccak256 calculates and returns the Keccak256 hash of the input data. // Keccak256 calculates and returns the Keccak256 hash of the input data.
func Keccak256(data ...[]byte) []byte { func Keccak256(data ...[]byte) []byte {
b := make([]byte, 32) b := make([]byte, 32)
d := hasherPool.Get().(KeccakState) hashInto(b, data...)
d.Reset()
for _, b := range data {
d.Write(b)
}
d.Read(b)
hasherPool.Put(d)
return b return b
} }
// Keccak256Hash calculates and returns the Keccak256 hash of the input data, // Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// converting it to an internal Hash data structure. // converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) (h common.Hash) { func Keccak256Hash(data ...[]byte) (h common.Hash) {
hashInto(h[:], data...)
return h
}
func hashInto(out []byte, data ...[]byte) {
d := hasherPool.Get().(KeccakState) d := hasherPool.Get().(KeccakState)
defer hasherPool.Put(d)
d.Reset() d.Reset()
for _, b := range data { for _, b := range data {
d.Write(b) d.Write(b)
} }
d.Read(h[:]) d.Read(out)
hasherPool.Put(d)
return h
} }