Sample code for 30+ languages & platforms
Lua

CAdES BES Detached Signature

See more Encryption Examples

Demonstrates how to create a CAdES BES detached signature file (.p7s).

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 crypt = chilkat.Crypt2.new()

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

local cert = chilkat.Cert.new()
local success = cert:load_pfx_file(pfxPath, pfxPassword)
if not success then
    print(cert.last_error_text)
    return
end

-- Tell the crypt component to use this cert.
success = crypt:set_signing_cert(cert)
if not success then
    print(crypt.last_error_text)
    return
end

-- The CadesEnabled property applies to all methods that create PKCS7 signatures.
-- To create a CAdES-BES signature, set this property equal to true.
crypt.cades_enabled = true

-- 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"

-- Create the detached CAdES-BES signature:
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!")