mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-22 19:50:36 +00:00
57bcbaa21f
* Correct naming of hash func in Eth2 * Customizable mode of operation for Caplin
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
/*
|
|
Copyright 2022 Erigon-Lightclient contributors
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package utils
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"hash"
|
|
"sync"
|
|
)
|
|
|
|
type HashFunc func(data []byte, extras ...[]byte) [32]byte
|
|
|
|
var hasherPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return sha256.New()
|
|
},
|
|
}
|
|
|
|
// General purpose Sha256
|
|
func Sha256(data []byte, extras ...[]byte) [32]byte {
|
|
h, ok := hasherPool.Get().(hash.Hash)
|
|
if !ok {
|
|
h = sha256.New()
|
|
}
|
|
defer hasherPool.Put(h)
|
|
h.Reset()
|
|
|
|
var b [32]byte
|
|
|
|
h.Write(data)
|
|
for _, extra := range extras {
|
|
h.Write(extra)
|
|
}
|
|
h.Sum(b[:0])
|
|
return b
|
|
}
|
|
|
|
// Optimized Sha256, avoid pool.put/pool.get, meant for intensive operations.
|
|
// this version is not thread safe
|
|
func OptimizedSha256NotThreadSafe() HashFunc {
|
|
h := sha256.New()
|
|
var b [32]byte
|
|
return func(data []byte, extras ...[]byte) [32]byte {
|
|
h.Reset()
|
|
h.Write(data)
|
|
for _, extra := range extras {
|
|
h.Write(extra)
|
|
}
|
|
h.Sum(b[:0])
|
|
return b
|
|
}
|
|
}
|