mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
crypto: refactor deduplicate hashing logic
This commit is contained in:
parent
5e6f7374de
commit
4c753498b2
1 changed files with 8 additions and 10 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue