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
+18.7k Unmarshal/Load CSV record into struct in Go
+5.9k Golang : Denco multiplexer example
+12.3k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+35.5k Golang : Smarter Error Handling with strings.Contains()
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+6.2k Golang & Javascript : How to save cropped image to file on server
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+4.9k JQuery : Calling a function inside Jquery(document) block
+9.5k Golang : Get all countries currencies code in JSON format
+8.2k Golang : Reverse text lines or flip line order example
+14.4k Golang : Parsing or breaking down URL