Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua

    -- In the following call to loadlib, change the path (./chilkat.dll) to the relative or absolute directory where the chilkat.dll, chilkat.so, or chilkat.dylib is located.
    chilkat = assert(package.loadlib("./chilkat.dll", "luaopen_chilkat"))()
    print(chilkat._VERSION)

    local success = false

    --  For this example, I have a certificate in raw base64 format (not PEM),
    --  that looks like this:  "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
    local sbCertBase64 = chilkat.newStringBuilder{}
    success = sbCertBase64:LoadFile("qa_data/certs/base64Cert.txt","utf-8")

    local cert = chilkat.newCert{}
    success = cert:LoadFromBase64(sbCertBase64:GetAsString())
    if success == false then
        print(cert:LastErrorText())

    end

    --  Get the public key.
    local pubKey = chilkat.newPublicKey{}
    cert:GetPublicKey(pubKey)

    local numBits = pubKey:KeySize()
    print("Number of bits = ", numBits)

    --  If using an older version of Chilkat, the key size can be obtained like this:
    local xml = chilkat.newXml{}
    xml:LoadXml(pubKey:GetXml())

    local binDat = chilkat.newBinData{}
    binDat:AppendEncoded(xml:GetChildContent("Modulus"),"base64")

    numBits = 8 * binDat:NumBytes()
    print("Number of bits = ", numBits)