Golang crypto/x509.ParsePKIXPublicKey function example
package crypto/x509
ParsePKIXPublicKey parses a DER encoded public key. These values are typically found in PEM blocks with "BEGIN PUBLIC KEY".
Golang crypto/x509.ParsePKIXPublicKey function usage example
package main
import (
"crypto/x509"
"crypto/rsa"
"os"
"fmt"
"encoding/pem"
)
func main() {
var pemBytes = `-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAufyd1F3Xqe0htJGR49cV
aqvbGwYcmWsfKZUjB4Ijrk3wreidW3xdM0gEF828An5/sWiOT0BjgGyPL3I/GP/F
bFRkzTFWgaejmnnSz7OVvm5sCoep8n0W9ZTtudjSdHGjUhW5hagFAjyC2C6unbe0
ZELR0v1L1JB8ygvLiMpY97MeZCJ455gI78nvjQDZbmCJUhY+hQABC9XM8mHVSSY6
kZ0ZkkQ47m17H40EV/wtClyr84uQIZekBlnbC4jJVSI9AMRG5J6Pd2Uj+I7VOqco
ryBtCUfIrwjns/YdyvQDfmpR2iFUahuQDFgQzJznlutsXAo/l6kNYPj1zcnhH1zM
vwIDAQAB
-----END RSA PUBLIC KEY-----`
block, _ := pem.Decode([]byte(pemBytes))
publickey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// convert publickey interface to rsa.PublicKey type
rsaPubKey := publickey.(*rsa.PublicKey)
fmt.Printf("Public Key N value(modulus) : : %d\n\n", rsaPubKey.N)
fmt.Printf("Public Key E value(exponent) : : %d\n\n", rsaPubKey.E)
}
Output :
Public Key N value(modulus) : : 2347865626835....4959
Public Key E value(exponent) : : 65537
Reference :
Advertisement
Something interesting
Tutorials
+11.6k Get form post value in Go
+5.2k Golang : Convert lines of string into list for delete and insert operation
+9.2k Golang : How to check if a string with spaces in between is numeric?
+6.7k Golang : Output or print out JSON stream/encoded data
+22.4k Golang : Read directory content with filepath.Walk()
+12.5k Golang : Forwarding a local port to a remote server example
+9.4k Golang : How to protect your source code from client, hosting company or hacker?
+19k Golang : Padding data for encryption and un-padding data for decryption
+13.6k Golang : Get user input until a command or receive a word to stop
+7k Golang : Takes a plural word and makes it singular
+11.8k Golang : convert(cast) float to string
+5.3k Javascript : Change page title to get viewer attention