github.com/Team-Kujira/tendermint@v0.34.24-indexer/crypto/tmhash/hash.go (about)

     1  package tmhash
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"hash"
     6  )
     7  
     8  const (
     9  	Size      = sha256.Size
    10  	BlockSize = sha256.BlockSize
    11  )
    12  
    13  // New returns a new hash.Hash.
    14  func New() hash.Hash {
    15  	return sha256.New()
    16  }
    17  
    18  // Sum returns the SHA256 of the bz.
    19  func Sum(bz []byte) []byte {
    20  	h := sha256.Sum256(bz)
    21  	return h[:]
    22  }
    23  
    24  //-------------------------------------------------------------
    25  
    26  const (
    27  	TruncatedSize = 20
    28  )
    29  
    30  type sha256trunc struct {
    31  	sha256 hash.Hash
    32  }
    33  
    34  func (h sha256trunc) Write(p []byte) (n int, err error) {
    35  	return h.sha256.Write(p)
    36  }
    37  func (h sha256trunc) Sum(b []byte) []byte {
    38  	shasum := h.sha256.Sum(b)
    39  	return shasum[:TruncatedSize]
    40  }
    41  
    42  func (h sha256trunc) Reset() {
    43  	h.sha256.Reset()
    44  }
    45  
    46  func (h sha256trunc) Size() int {
    47  	return TruncatedSize
    48  }
    49  
    50  func (h sha256trunc) BlockSize() int {
    51  	return h.sha256.BlockSize()
    52  }
    53  
    54  // NewTruncated returns a new hash.Hash.
    55  func NewTruncated() hash.Hash {
    56  	return sha256trunc{
    57  		sha256: sha256.New(),
    58  	}
    59  }
    60  
    61  // SumTruncated returns the first 20 bytes of SHA256 of the bz.
    62  func SumTruncated(bz []byte) []byte {
    63  	hash := sha256.Sum256(bz)
    64  	return hash[:TruncatedSize]
    65  }