Swift
Swift
Get a Certificate's Key Size
See more Certificates Examples
Demonstrates how to get the RSA key size of a certificate (for example, 1024-bit, 2048-bit, etc.)Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
let sbCertBase64 = CkoStringBuilder()!
success = sbCertBase64.loadFile(path: "qa_data/certs/base64Cert.txt", charset: "utf-8")
let cert = CkoCert()!
success = cert.load(fromBase64: sbCertBase64.getAsString())
if success == false {
print("\(cert.lastErrorText!)")
return
}
// Get the public key.
let pubKey = CkoPublicKey()!
cert.getPublicKey(pubKey: pubKey)
var numBits: Int = pubKey.keySize.intValue
print("Number of bits = \(numBits)")
// If using an older version of Chilkat, the key size can be obtained like this:
let xml = CkoXml()!
xml.load(xmlData: pubKey.getXml())
let binDat = CkoBinData()!
binDat.appendEncoded(encData: xml.getChildContent(tagPath: "Modulus"), encoding: "base64")
numBits = 8 * binDat.numBytes.intValue
print("Number of bits = \(numBits)")
}