Standard library / crypto/x509

crypto/x509.MarshalECPrivateKey()

Serializes a SEC 1 EC private key into DER. Private DER contains secret material; never log it. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error)

Parameters & Attributes

NameTypeDescription
key*ecdsa.PrivateKeyKey bytes; see description for size.

Return Value

([]byte, error)

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

Working Example

Example Snippetgo
package main
import (  "crypto/ecdsa"  "crypto/elliptic"  "crypto/rand"  "crypto/x509"  "fmt")
func main() {  key := must(ecdsa.GenerateKey(elliptic.P256(), rand.Reader))  der := must(x509.MarshalECPrivateKey(key))  out := must(x509.ParseECPrivateKey(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