github.com/boxboat/in-toto-golang@v0.0.3-0.20210303203820-2fa16ecbe6f6/in_toto/hashlib.go (about)

     1  package in_toto
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"crypto/sha512"
     6  	"hash"
     7  )
     8  
     9  /*
    10  getHashMapping returns a mapping from hash algorithm to supported hash
    11  interface.
    12  */
    13  func getHashMapping() map[string]func() hash.Hash {
    14  	return map[string]func() hash.Hash{
    15  		"sha256": sha256.New,
    16  		"sha512": sha512.New,
    17  		"sha384": sha512.New384,
    18  	}
    19  }
    20  
    21  /*
    22  hashToHex calculates the hash over data based on hash algorithm h.
    23  */
    24  func hashToHex(h hash.Hash, data []byte) []byte {
    25  	h.Write(data)
    26  	// We need to use h.Sum(nil) here, because otherwise hash.Sum() appends
    27  	// the hash to the passed data. So instead of having only the hash
    28  	// we would get: "dataHASH"
    29  	return h.Sum(nil)
    30  }