Standard library / crypto/rsa

crypto/rsa.SignPKCS1v15()

Creates deterministic PKCS#1 v1.5 signatures of a prehashed message. Use only when required by protocol; prefer PSS for new RSA signature designs. Random argument is ignored in Go 1.23. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func SignPKCS1v15(random io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error)

Parameters & Attributes

NameTypeDescription
randomio.ReaderCryptographically secure random source; see description for exceptions.
priv*PrivateKeyPrivate key used for the operation.
hashcrypto.HashHash algorithm, hash instance or digest as specified by the signature and description.
hashed[]byteDigest of the message.

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"))  s := must(rsa.SignPKCS1v15(rand.Reader, k, crypto.SHA256, h[:]))  fmt.Println(len(s) == k.Size())}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