import ( "crypto/rsa" "crypto/rand" "crypto" ) func signRSA(data []byte, privateKey *rsa.PrivateKey) ([]byte, error) { // generate the SHA256 hash of the data hash := crypto.SHA256.New() hash.Write(data) digest := hash.Sum(nil) // sign the hash using the private key signature, err := privateKey.Sign(rand.Reader, digest, crypto.SHA256) if err != nil { return nil, err } return signature, nil }
import ( "crypto/ecdsa" "crypto/rand" "crypto" ) func signECDSA(data []byte, privateKey *ecdsa.PrivateKey) ([]byte, error) { // generate the SHA256 hash of the data hash := crypto.SHA256.New() hash.Write(data) digest := hash.Sum(nil) // sign the hash using the private key signature, err := ecdsa.SignASN1(rand.Reader, privateKey, digest) if err != nil { return nil, err } return signature, nil }In this example, the ECDSA signer generates a digital signature for some data using a private key. Package Library: go.crypto/Signer/Public.