Standard library / crypto/x509

crypto/x509.MarshalPKCS1PublicKey()

Serializes a PKCS #1 RSA public key into DER. Parsing does not establish trust. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte

Parameters & Attributes

NameTypeDescription
key*rsa.PublicKeyKey bytes; see description for size.

Return Value

[]byte

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.MarshalPKCS1PublicKey(&key.PublicKey)  out := must(x509.ParsePKCS1PublicKey(der))  fmt.Println(out.Equal(&key.PublicKey))}
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