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

Sign a File to Create a .p7s (Detached Signature)

See more Encryption Examples

_LANGUAGE_ example to create a detached signature file (.p7s) for any type file. The signature can be verified by calling VerifyP7S and passing the original filename and the .p7s filename.

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()

-- Use a digital certificate and private key from a PFX file (.pfx or .p12).
local signingCertSubject = "Acme Inc"
local pfxFilename = "/Users/chilkat/testData/pfx/acme.pfx"
local pfxPassword = "test123"

local certStore = chilkat.CertStore.new()
local success = certStore:load_pfx_file(pfxFilename, pfxPassword)
if not success then
    print(certStore.last_error_text)
    return
end

local jsonCN = chilkat.JsonObject.new()
jsonCN:update_string("CN", signingCertSubject)

local cert = chilkat.Cert.new()
success = certStore:find_cert(jsonCN, cert)
if not success then
    print("Failed to find certificate by subject common name.")
    return
end

-- Tell the crypt component to use this cert.
success = crypt:set_signing_cert(cert)

-- We can sign any type of file, creating a .p7s as output:
local inFile = "/Users/chilkat/testData/pdf/sample.pdf"
local sigFile = "/Users/chilkat/testData/p7s/sample.p7s"

success = crypt:create_p7_s(inFile, sigFile)
if not success then
    print(crypt.last_error_text)
    return
end

success = crypt:verify_p7_s(inFile, sigFile)
if not success then
    print(crypt.last_error_text)
    return
end

print("Success!")