github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/prime256v1/hash.go (about)

     1  // Package prime256v1
     2  //
     3  // @author: xwc1125
     4  package prime256v1
     5  
     6  import (
     7  	"crypto"
     8  	"crypto/sha256"
     9  	"crypto/sha512"
    10  	"hash"
    11  )
    12  
    13  // HashFunc get hash.Hash
    14  func HashFunc(cryptoName string) func() hash.Hash {
    15  	switch cryptoName {
    16  	case "P-256":
    17  		return sha256.New
    18  	case "P-384":
    19  		return sha512.New384
    20  	case "P-521":
    21  		return sha512.New
    22  	default:
    23  		return nil
    24  	}
    25  }
    26  
    27  // Sha256 sha256
    28  func Sha256(data []byte) []byte {
    29  	h := sha256.New()
    30  	h.Write(data)
    31  	return h.Sum(nil)
    32  }
    33  
    34  // Sha384 Sha384
    35  func Sha384(data []byte) []byte {
    36  	h := sha512.New384()
    37  	h.Write(data)
    38  	return h.Sum(nil)
    39  }
    40  
    41  // Sha512 Sha512
    42  func Sha512(data []byte) []byte {
    43  	h := sha512.New()
    44  	h.Write(data)
    45  	return h.Sum(nil)
    46  }
    47  
    48  // HashMsg hash data
    49  func HashMsg(curveName string, msg []byte) []byte {
    50  	switch curveName {
    51  	case "P-256":
    52  		return Sha256(msg)
    53  	case "P-384":
    54  		return Sha384(msg)
    55  	case "P-521":
    56  		return Sha512(msg)
    57  	default:
    58  		return nil
    59  	}
    60  }
    61  
    62  // HashType hash type
    63  func HashType(curveName string) crypto.Hash {
    64  	switch curveName {
    65  	case "P-256":
    66  		return crypto.SHA256
    67  	case "P-384":
    68  		return crypto.SHA384
    69  	case "P-521":
    70  		return crypto.SHA512
    71  	default:
    72  		return crypto.SHA256
    73  	}
    74  }