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.
|
// 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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue