Standard library / crypto/x509
crypto/x509.CreateCertificateRequest()
Signs a PKCS#10 request with the requester private key. A CSR proves possession of a key, not ownership of a requested DNS name. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv any) (csr []byte, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| rand | io.Reader | Cryptographically secure random source; see description for exceptions. |
| template | *CertificateRequest | Template specifying the certificate, request or revocation list fields. |
| priv | any | Private signing key; must match issuer or requester public key as appropriate. |
Return Value
(csr []byte, err error)
Result as described above; check any returned error before use.
Working Example
Example Snippetgo
package main
import ( "crypto/ed25519" "crypto/rand" "crypto/x509" "fmt")
func main() { _, key, e := ed25519.GenerateKey(rand.Reader) check(e) der := must(x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{DNSNames: []string{"example.invalid"}}, key)) fmt.Println(must(x509.ParseCertificateRequest(der)).CheckSignature() == 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) }}