Standard library / crypto/rsa

crypto/rsa.PrivateKey.Sign()

Implements crypto.Signer. PSSOptions selects PSS; other signer options select PKCS#1 v1.5. Pass a digest, not raw message bytes. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error)

Parameters & Attributes

NameTypeDescription
randio.ReaderCryptographically secure random source; see description for exceptions.
digest[]byteDigest of the message.
optscrypto.SignerOptsAlgorithm-specific options; see description for nil behavior.

Return Value

([]byte, error)

Result as described above; check any returned error before use.

Working Example

Example Snippetgo
package main
import (  "crypto"  "crypto/rand"  "crypto/rsa"  "crypto/sha256"  "fmt")
func main() {  k := must(rsa.GenerateKey(rand.Reader, 2048))  h := sha256.Sum256([]byte("Go"))  o := &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash, Hash: crypto.SHA256}  s := must(k.Sign(rand.Reader, h[:], o))  fmt.Println(rsa.VerifyPSS(&k.PublicKey, crypto.SHA256, h[:], s, o) == nil)}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference