Lua Requires Chilkat v11.0.0+
Lua
RSA Signature with Certificate's Private Key from PFX
See more RSA Examples
Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Create an instance of a certificate store object, load a PFX file,
-- locate the certificate we need, and use it for signing.
-- (a PFX file may contain more than one certificate.)
local certStore = chilkat.CertStore.new()
-- The 1st argument is the filename, the 2nd arg is the
-- PFX file's password:
local success = certStore:load_pfx_file("chilkat.pfx", "test")
if not success then
print(certStore.last_error_text)
return
end
-- Find the certificate by the subject common name:
local jsonCN = chilkat.JsonObject.new()
jsonCN:update_string("CN", "cert common name")
local cert = chilkat.Cert.new()
success = certStore:find_cert(jsonCN, cert)
if not success then
print(certStore.last_error_text)
return
end
local privKey = chilkat.PrivateKey.new()
success = cert:get_private_key(privKey)
if not success then
print(cert.last_error_text)
return
end
local rsa = chilkat.Rsa.new()
success = rsa:use_private_key(privKey)
if not success then
print(rsa.last_error_text)
return
end
-- Encode the signature as a hex string
rsa.encoding_mode = "hex"
local strData = "This is the string to be signed."
-- Sign the string using the sha-1 hash algorithm.
-- Other valid choices are "sha-256", "md2" and "md5".
local hexSig = rsa:sign_string_enc(strData, "sha-1")
print(hexSig)
print("Success!")