Lua Requires Chilkat v11.0.0+
Lua
Iterate Keys and Certs in PEM
See more PEM Examples
Demonstrates how to access each of the private keys and certs contained within a PEM.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local pem = chilkat.Pem.new()
-- Load the PEM from a file.
-- If the PEM is encrypted, provide a password. Otherwise pass an empty string for the password.
local password = "myPassword"
local success = pem:load_pem_file("../myPemFiles/myPem.pem", password)
if not success then
print(pem.last_error_text)
return
end
-- Note: If the app already has the PEM pre-loaded in a string variable, then load it
-- by calling LoadPem instead.
local pemContent = "... the PEM contents ..."
success = pem:load_pem(pemContent, password)
-- Check for success as before..
-- Iterate over the private keys.
local numPrivateKeys = pem.num_private_keys
local i = 0
local privKey = chilkat.PrivateKey.new()
while i < numPrivateKeys do
pem:private_key_at(i, privKey)
print("Private Key " .. i .. " is " .. privKey.bit_length .. " in length")
i = i + 1
end
-- Iterate over the certificates.
local cert = chilkat.Cert.new()
local numCerts = pem.num_certs
i = 0
while i < numCerts do
pem:cert_at(i, cert)
print("Certificate " .. i .. " : " .. cert.subject_dn)
i = i + 1
end