Golang : Load ASN1 encoded DSA public key PEM file example



Tags : golang DSA private-key public-key pem-block

In this tutorial, we will learn how to get the DSA public key from a ASN1 encoded PEM file generated from previous tutorial. Pretty straight forward examples. Hope it helps!

From file :

 package main

 import (
 "bufio"
 "crypto/dsa"
 "encoding/asn1"
 "encoding/pem"
 "fmt"
 "os"
 )

 func main() {
 //load DSA PEM encoded public key file
 pemFile, err := os.Open("DSApublickey.pem")

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

 // need to convert PEM file to []byte for decoding

 pemFileInfo, _ := pemFile.Stat()
 var size int64 = pemFileInfo.Size()
 pemBytes := make([]byte, size)

 // read pemFile content into pemBytes
 buffer := bufio.NewReader(pemFile)
 _, err = buffer.Read(pemBytes)

 // proper decoding now
 pemBlock, _ := pem.Decode([]byte(pemBytes))

 pemFile.Close()

 // convert PEM block to dsa.PublicKey
 // we use ASN1 because the PEM block was encoded with ASN1
 // see https://www.socketloop.com/tutorials/golang-generate-dsa-private-public-key-and-pem-files-example

 // if the PEM block is not encoded by ASN1, the unmarshal will fail
 // you need to find out the Public Key algorithm used to encode the PEM bytes

 var publicKey dsa.PublicKey

 _, err = asn1.Unmarshal(pemBlock.Bytes, &publicKey)

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

 //fmt.Printf("Public key : \n%x\n", publicKey)

 fmt.Printf("Public key parameter P: %v\n", publicKey.Parameters.P)
 fmt.Printf("Public key parameter Q: %v\n", publicKey.Parameters.Q)
 fmt.Printf("Public key parameter G: %v\n", publicKey.Parameters.G)
 fmt.Printf("Public key Y: %v\n", publicKey.Y)

 }

and from PEM block :

 package main

 import (
 "crypto/dsa"
 "encoding/asn1"
 "encoding/pem"
 "fmt"
 "os"
 )

 func main() {

 var pemBytes = `
 -----BEGIN PUBLIC KEY-----
 MIIBpjCCAR4CgYEA1cKOQUxDRqRHt8yR5vfEunyFB6pblE9W/fyaJHgpWMzkvSHX
 mZvDhN5huH3OM0vC5Y8UbfyplET3x/HfXUbDUgk4bT0CrWHmrANMjdPgStZF+nWP
 Yfa6QUyVbRZumI6iBaCH63107scE8tygmwSW3n1jYLoSv6VItDEiBIdoK18CFQC6
 q4LlyX4YZblOKYw8CFyPShtcAwKBgHAN+TWyUhqCVZmwUdH3pJelT4iT9vkg4NLn
 1h+qJJ1XU+OILAAeuO3z8vLMIpeFaDL5CvUb7S0vSqx2EFj/G67aH9nL0MwtXjn7
 SCy4EOF5dlHbafXj4PnPrvo3/Mr+3a2i5lenlhyyb1Vnd/0VcrGwWleAfDBuGdYu
 S5WCYAj3AoGBAMDl+N8XI3LBi/LUQbi9di0tvA/2t+c6UZTT/CDTkyDucFNEeqWI
 sdOsf+hIbI8pEy81y6yBc50wcf1uqcZxovKsZbuv8vS3NBPaeOT7l6ltYdNxzg/7
 QFfi3qQXXLONWYXW4diWaZu6Kq5XvhfWkoUdGzGiD84UVW7jmeDy/Px6
 -----END PUBLIC KEY-----
 `

 // proper decoding now
 pemBlock, _ := pem.Decode([]byte(pemBytes))

 // convert PEM block to dsa.PublicKey
 // we use ASN1 because the PEM block was encoded with ASN1
 // see https://www.socketloop.com/tutorials/golang-generate-dsa-private-public-key-and-pem-files-example

 // if the PEM block is not encoded by ASN1, the unmarshal will fail
 // you need to find out the Public Key algorithm used to encode the PEM bytes

 var publicKey dsa.PublicKey

 _, err := asn1.Unmarshal(pemBlock.Bytes, &publicKey)

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

 //fmt.Printf("Public key : \n%x\n", publicKey)

 fmt.Printf("Public key parameter P: %v\n", publicKey.Parameters.P)
 fmt.Printf("Public key parameter Q: %v\n", publicKey.Parameters.Q)
 fmt.Printf("Public key parameter G: %v\n", publicKey.Parameters.G)
 fmt.Printf("Public key Y: %v\n", publicKey.Y)

 }

  See also : Golang : Generate DSA private, public key and PEM files example



Tags : golang DSA private-key public-key pem-block

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement