Golang crypto/x509.MarshalPKIXPublicKey function example

package crypto/x509

MarshalPKIXPublicKey serialises a public key to DER-encoded PKIX format.

Golang crypto/x509.MarshalPKIXPublicKey function usage example

 package main

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

 func main() {

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

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

 var publickey rsa.PublicKey
 publickey = privatekey.PublicKey

 der, err := x509.MarshalPKIXPublicKey(&publickey)

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

 var fp [sha256.Size]byte
 fp = sha256.Sum256(der)
 fmt.Printf("%s\n", hex.EncodeToString(fp[:8]))
 }

Reference :

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

Advertisement