Go
Go
Get Public Key from CSR
See more CSR Examples
Demonstrates how to get the public key from a CSR.Chilkat Go Downloads
success := false
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
pem := chilkat.NewPem()
// No password is required. Pass an empty password string..
noPassword := ""
success = pem.LoadPemFile("qa_data/csr/csr2.pem",noPassword)
if success != true {
fmt.Println(pem.LastErrorText())
pem.DisposePem()
return
}
strBase64 := pem.GetEncodedItem("csr","","base64",0)
asn := chilkat.NewAsn()
success = asn.LoadEncoded(*strBase64,"base64")
if success != true {
fmt.Println(asn.LastErrorText())
pem.DisposePem()
asn.DisposeAsn()
return
}
// Convert the ASN.1 to XML.
xml := chilkat.NewXml()
success = xml.LoadXml(*asn.AsnToXml())
fmt.Println(*xml.GetXml())
fmt.Println("----")
strModulusHex := xml.GetChildContent("bits")
fmt.Println("strModulusHex = ", *strModulusHex)
fmt.Println("----")
// We need the modulus as base64.
bd := chilkat.NewBinData()
bd.AppendEncoded(*strModulusHex,"hex")
modulus64 := bd.GetEncoded("base64")
fmt.Println("modulus64 = ", *modulus64)
fmt.Println("----")
// Build the XML for the public key.
xmlPubKey := chilkat.NewXml()
xmlPubKey.SetTag("RSAPublicKey")
xmlPubKey.UpdateChildContent("Modulus",*modulus64)
// The RSA exponent will always be decimal 65537 (base64 = AQAB)
xmlPubKey.UpdateChildContent("Exponent","AQAB")
fmt.Println("RSA public key as XML:")
fmt.Println(*xmlPubKey.GetXml())
fmt.Println("----")
// Load the XML into a Chilkat public key object.
pubkey := chilkat.NewPublicKey()
success = pubkey.LoadFromString(*xmlPubKey.GetXml())
if success != true {
fmt.Println(pubkey.LastErrorText())
pem.DisposePem()
asn.DisposeAsn()
xml.DisposeXml()
bd.DisposeBinData()
xmlPubKey.DisposeXml()
pubkey.DisposePublicKey()
return
}
// Show the public key as PEM.
preferPkcs1 := true
fmt.Println(*pubkey.GetPem(preferPkcs1))
pem.DisposePem()
asn.DisposeAsn()
xml.DisposeXml()
bd.DisposeBinData()
xmlPubKey.DisposeXml()
pubkey.DisposePublicKey()