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

RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature

See more RSA Examples

Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local rsa = chilkat.Rsa.new()

-- Load the .pfx/.p12
local pfx = chilkat.Pfx.new()
local success = pfx:load_pfx_file("qa_data/pfx/myKey.p12", "myPassword")
if not success then
    print(pfx.last_error_text)
    return
end

-- Get the default private key.

local privKey = chilkat.PrivateKey.new()
success = pfx:private_key_at(0, privKey)
if not success then
    print(pfx.last_error_text)
    return
end

-- Import the private key into the RSA component:
success = rsa:use_private_key(privKey)
if not success then
    print(rsa.last_error_text)
    return
end

-- Get the signature in base64
rsa.encoding_mode = "base64"

local strData = "This is the string to be signed."

-- Sign the string using the sha256 hash algorithm.
-- Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
local base64Sig = rsa:sign_string_enc(strData, "sha256")

print(base64Sig)

print("Success!")