Golang crypto/x509.MarshalPKCS1PrivateKey function example

package crypto/x509

MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.

Golang crypto/x509.MarshalPKCS1PrivateKey function usage example

 package main

  import (
 "fmt"
 "crypto/rand"
 "crypto/rsa"
 "crypto/x509"
 "encoding/pem"
 "os"
  )

  func main() {

 privatekey, err := rsa.GenerateKey(rand.Reader, 1024)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 var marshalledPKCS1privkey []byte

 marshalledPKCS1privkey = x509.MarshalPKCS1PrivateKey(privatekey)

 // ASN.1 DER encoded form
 pemBlock := pem.Block{
 Type : "RSA PRIVATE KEY",
 Headers : nil,
 Bytes : marshalledPKCS1privkey,
 }

 fmt.Printf("%v\n", pemBlock)

  }

Reference :

https://www.socketloop.com/tutorials/golang-example-for-rsa-package-functions-example

http://golang.org/pkg/crypto/x509/#MarshalPKCS1PrivateKey

Advertisement