Standard library / crypto/x509

crypto/x509.ParsePKCS1PrivateKey()

Parses DER containing a PKCS #1 RSA private key. Reject malformed data and check the returned concrete key type when applicable. Private DER contains secret material; never log it. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error)

Parameters & Attributes

NameTypeDescription
der[]byteASN.1 DER bytes in the required format.

Return Value

(*rsa.PrivateKey, error)

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

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "crypto/rsa"  "crypto/x509"  "fmt")
func main() {  key := must(rsa.GenerateKey(rand.Reader, 2048))  der := x509.MarshalPKCS1PrivateKey(key)  out := must(x509.ParsePKCS1PrivateKey(der))  fmt.Println(out.Equal(key))}
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