Standard library / crypto/x509
crypto/x509.MarshalPKCS8PrivateKey()
Serializes a unencrypted PKCS #8 private key into DER. Private DER contains secret material; never log it. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func MarshalPKCS8PrivateKey(key any) ([]byte, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | any | Key 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/ed25519" "crypto/rand" "crypto/x509" "fmt")
func main() { pub, key, e := ed25519.GenerateKey(rand.Reader) check(e) _, _ = pub, key der := must(x509.MarshalPKCS8PrivateKey(key)) out := must(x509.ParsePKCS8PrivateKey(der)).(ed25519.PrivateKey) 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) }}