github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/crypto/sha3pool/pool.go (about)

     1  // Package sha3pool is a freelist for SHA3-256 hash objects.
     2  package sha3pool
     3  
     4  import (
     5  	"sync"
     6  
     7  	"golang.org/x/crypto/sha3"
     8  )
     9  
    10  var pool = &sync.Pool{New: func() interface{} { return sha3.New256() }}
    11  
    12  // Get256 returns an initialized SHA3-256 hash ready to use.
    13  // It is like sha3.New256 except it uses the freelist.
    14  // The caller should call Put256 when finished with the returned object.
    15  func Get256() sha3.ShakeHash {
    16  	return pool.Get().(sha3.ShakeHash)
    17  }
    18  
    19  // Put256 resets h and puts it in the freelist.
    20  func Put256(h sha3.ShakeHash) {
    21  	h.Reset()
    22  	pool.Put(h)
    23  }
    24  
    25  // Sum256 uses a ShakeHash from the pool to sum into hash.
    26  func Sum256(hash, data []byte) {
    27  	h := Get256()
    28  	h.Write(data)
    29  	h.Read(hash)
    30  	Put256(h)
    31  }