Lua Requires Chilkat v11.0.0+
Lua
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
local chilkat = require("chilkat")
-- For this example, I have a certificate in raw base64 format (not PEM),
-- that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
local sbCertBase64 = chilkat.StringBuilder.new()
local success = sbCertBase64:load_file("qa_data/certs/base64Cert.txt", "utf-8")
local cert = chilkat.Cert.new()
success = cert:load_from_base64(sbCertBase64:get_as_string())
if not success then
print(cert.last_error_text)
return
end
-- Get the public key.
local pubKey = chilkat.PublicKey.new()
cert:get_public_key(pubKey)
local numBits = pubKey.key_size
print("Number of bits = " .. numBits)
-- If using an older version of Chilkat, the key size can be obtained like this:
local xml = chilkat.Xml.new()
xml:load_xml(pubKey:get_xml())
local binDat = chilkat.BinData.new()
binDat:append_encoded(xml:get_child_content("Modulus"), "base64")
numBits = 8 * binDat.num_bytes
print("Number of bits = " .. numBits)