Standard library / crypto/x509

crypto/x509.ParsePKIXPublicKey()

Parses DER containing a PKIX SubjectPublicKeyInfo public key. Reject malformed data and check the returned concrete key type when applicable. Parsing does not establish trust. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func ParsePKIXPublicKey(derBytes []byte) (pub any, err error)

Parameters & Attributes

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

Return Value

(pub any, 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() {  pub, key, e := ed25519.GenerateKey(rand.Reader)  check(e)  _, _ = pub, key  der := must(x509.MarshalPKIXPublicKey(pub))  out := must(x509.ParsePKIXPublicKey(der)).(ed25519.PublicKey)  fmt.Println(out.Equal(pub))}
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