Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oCrypt
LOCAL oCert
LOCAL oCmsOptions
LOCAL oJsonSigningAttrs
LOCAL cPathOfFileToSign
LOCAL cOutputPath
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oCrypt := CreateObject("Chilkat.Crypt2")
oCert := CreateObject("Chilkat.Cert")
// 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..
nSuccess := oCert:LoadFromSmartcard("")
IF (nSuccess != 1)
? oCert:LastErrorText
oCrypt:destroy()
oCert:destroy()
RETURN
ENDIF
// Tell the crypt component to use this cert.
nSuccess := oCrypt:SetSigningCert(oCert)
IF (nSuccess != 1)
? oCrypt:LastErrorText
oCrypt:destroy()
oCert:destroy()
RETURN
ENDIF
oCmsOptions := CreateObject("Chilkat.JsonObject")
oCmsOptions:UpdateBool("DigestData", 1)
oCmsOptions:UpdateBool("OmitAlgorithmIdNull", 1)
oCrypt:CmsOptions := oCmsOptions: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.
oCrypt:CadesEnabled := 1
oCrypt:HashAlgorithm := "sha256"
oJsonSigningAttrs := CreateObject("Chilkat.JsonObject")
oJsonSigningAttrs:UpdateInt("contentType", 1)
oJsonSigningAttrs:UpdateInt("signingTime", 1)
oJsonSigningAttrs:UpdateInt("messageDigest", 1)
oJsonSigningAttrs:UpdateInt("signingCertificateV2", 1)
oCrypt:SigningAttributes := oJsonSigningAttrs: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:
oCrypt:IncludeCertChain := 0
// Make sure we sign the utf-8 byte representation of the JSON string
oCrypt:Charset := "utf-8"
// Create the CAdES-BES signature, which does not contain the data being signed.
cPathOfFileToSign := "someDir/someFile"
cOutputPath := "outDir/someFile.p7s"
nSuccess := oCrypt:CreateP7S(cPathOfFileToSign, cOutputPath)
IF (nSuccess != 1)
? oCrypt:LastErrorText
oCrypt:destroy()
oCert:destroy()
oCmsOptions:destroy()
oJsonSigningAttrs:destroy()
RETURN
ENDIF
? "Success!"
oCrypt:destroy()
oCert:destroy()
oCmsOptions:destroy()
oJsonSigningAttrs:destroy()