Sample code for 30+ languages & platforms
Lua

Create Egypt ITIDA CAdES-BES .p7s Signature (File to File)

See more Egypt ITIDA Examples

Demonstrates how to create a .p7s signature that fits Egypt's ITIDA requirements.

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

local cert = chilkat.Cert.new()

-- There are many ways to load the certificate.
-- This example was created for a customer using an ePass2003 USB token.
-- Assuming the USB token is the only source of a hardware-based private key..
local success = cert:load_from_smartcard("")
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

local cmsOptions = chilkat.JsonObject.new()
cmsOptions:update_bool("DigestData", true)
cmsOptions:update_bool("OmitAlgorithmIdNull", true)
crypt.cms_options = cmsOptions:emit()

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

crypt.hash_algorithm = "sha256"

local jsonSigningAttrs = chilkat.JsonObject.new()
jsonSigningAttrs:update_int("contentType", 1)
jsonSigningAttrs:update_int("signingTime", 1)
jsonSigningAttrs:update_int("messageDigest", 1)
jsonSigningAttrs:update_int("signingCertificateV2", 1)
crypt.signing_attributes = jsonSigningAttrs:emit()

-- By default, all the certs in the chain of authentication are included in the signature.
-- If desired, we can choose to only include the signing certificate:
crypt.include_cert_chain = false

-- Make sure we sign the utf-8 byte representation of the JSON string
crypt.charset = "utf-8"

-- Create the CAdES-BES signature, which does not contain the data being signed.
local pathOfFileToSign = "someDir/someFile"
local outputPath = "outDir/someFile.p7s"
success = crypt:create_p7_s(pathOfFileToSign, outputPath)
if not success then
    print(crypt.last_error_text)
    return
end

print("Success!")