github.com/okex/exchain@v1.8.0/libs/tendermint/crypto/etherhash/hash.go (about)

     1  package etherhash
     2  
     3  import (
     4  	"hash"
     5  	"sync"
     6  
     7  	"golang.org/x/crypto/sha3"
     8  )
     9  
    10  var keccakPool = sync.Pool{
    11  	// NewLegacyKeccak256 uses non-standard padding
    12  	// and is incompatible with sha3.Sum256
    13  	New: func() interface{} { return sha3.NewLegacyKeccak256() },
    14  }
    15  
    16  type keccakState interface {
    17  	hash.Hash
    18  	Read([]byte) (int, error)
    19  }
    20  
    21  // Sum returns the non-standard Keccak256 of the bz.
    22  func Sum(bz []byte) []byte {
    23  	sha := keccakPool.Get().(keccakState)
    24  	defer func() {
    25  		// better to reset before putting it to the pool
    26  		sha.Reset()
    27  		keccakPool.Put(sha)
    28  	}()
    29  	sha.Write(bz)
    30  
    31  	var hashData [32]byte
    32  	sha.Read(hashData[:])
    33  	return hashData[:]
    34  }