Golang crypto/x509.MarshalECPrivateKey function example
package crypto/x509
MarshalECPrivateKey marshals an EC private key into ASN.1, DER format.
Golang crypto/x509.MarshalECPrivateKey function usage example
package main
import (
"fmt"
"crypto/rand"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/x509"
"encoding/pem"
"os"
)
func main() {
privatekey := new(ecdsa.PrivateKey)
curve := elliptic.P256()
privatekey, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var marshalledprivkey []byte
marshalledprivkey,err = x509.MarshalECPrivateKey(privatekey)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// ASN.1 DER encoded form
pemBlock := pem.Block{
Type : "RSA PRIVATE KEY",
Headers : nil,
Bytes : marshalledPKCS1privkey,
}
fmt.Printf("%v\n", pemBlock)
}
Reference :
Advertisement
Something interesting
Tutorials
+18.4k Golang : Logging with logrus
+23.5k Golang : Get ASCII code from a key press(cross-platform) example
+4.7k JavaScript: Add marker function on Google Map
+15k Golang : package is not in GOROOT during compilation
+17k Golang : How to save log messages to file?
+5.2k Golang : Issue HTTP commands to server and port example
+6.8k Golang : Join lines with certain suffix symbol example
+8.8k Golang : On lambda, anonymous, inline functions and function literals
+19.2k Golang : Populate dropdown with html/template example
+7.5k Golang : Get YouTube playlist
+24.5k Golang : GORM read from database example