Sample code for 30+ languages & platforms
Lua Requires Chilkat v10.1.2+

Create PKCS7 Signed File (.p7m)

See more Encryption Examples

Demonstrates how to sign a file to create a .p7m that contains both the file contents and the signature.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local crypt = chilkat.Crypt2.new()

local certStore = chilkat.CertStore.new()

-- Load a PFX file into a certificate store object.
local success = certStore:load_pfx_file("myPfx.pfx", "pfxPassword")
if not success then
    print(certStore.last_error_text)
    return
end

-- Get the certificate by subject common name.
-- This should be the cert within the PFX that also
-- has a private key (also stored within the PFX).
local jsonCN = chilkat.JsonObject.new()
jsonCN:update_string("CN", "myCert")
local cert = chilkat.Cert.new()
success = certStore:find_cert(jsonCN, cert)
if not success then
    print(certStore.last_error_text)
    return
end

-- Tell the crypt object to use the certificate for signing:
success = crypt:set_signing_cert(cert)

-- Sign a file, producing a .p7m as output.
-- The input file is unchanged, the test.p7m contains the
-- contents of the input file and the signature.
local inFile = "test.txt"
local outFile = "testp7m"
success = crypt:create_p7_m(inFile, outFile)
if not success then
    print(crypt.last_error_text)
    return
end

print("Success!")