Lua Requires Chilkat v10.1.2+
Lua
RSA Sign String using Private Key of Certificate Type A3 (smart card / token)
See more RSA Examples
Demonstrates RSA signing a string using the private key of a certificate type A3 (smart card, token).Note: This is a Windows-only example.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- First get the A3 certificate that was installed on the Windows system.
local certStore = chilkat.CertStore.new()
local thumbprint = "12c1dd8015f3f03f7b1fa619dc24e2493ca8b4b2"
-- This is specific to Windows because it is opening the Windows Current-User certificate store.
local bReadOnly = true
local success = certStore:open_current_user_store(bReadOnly)
if not success then
print(certStore.last_error_text)
return
end
-- Find the certificate with the desired thumbprint
-- (There are many ways to locate a certificate. This example chooses to find by thumbprint.)
local json = chilkat.JsonObject.new()
json:update_string("thumbprint", thumbprint)
local cert = chilkat.Cert.new()
success = certStore:find_cert(json, cert)
if not success then
print("Failed to find the certificate.")
return
end
print("Found: " .. cert.subject_cn)
local rsa = chilkat.Rsa.new()
-- Provide the cert's private key
local bUsePrivateKey = true
success = rsa:set_x509_cert(cert, bUsePrivateKey)
if not success then
print(rsa.last_error_text)
return
end
-- Return the RSA signature in base64 encoded form.
rsa.encoding_mode = "base64"
-- Sign the utf-8 byte representation of the string.
rsa.charset = "utf-8"
-- You can also choose other hash algorithms, such as SHA-1.
local sigBase64 = rsa:sign_string_enc("text to sign", "SHA-256")
if not rsa.last_method_success then
print(rsa.last_error_text)
return
end
print("Base64 signature: " .. sigBase64)