Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCrypt
LOCAL cSigningCertSubject
LOCAL cPfxFilename
LOCAL cPfxPassword
LOCAL oCertStore
LOCAL oJsonCN
LOCAL oCert
LOCAL cInFile
LOCAL cSigFile

nSuccess := 0

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

oCrypt := CreateObject("Chilkat.Crypt2")

//  Use a digital certificate and private key from a PFX file (.pfx or .p12).
cSigningCertSubject := "Acme Inc"
cPfxFilename := "/Users/chilkat/testData/pfx/acme.pfx"
cPfxPassword := "test123"

oCertStore := CreateObject("Chilkat.CertStore")
nSuccess := oCertStore:LoadPfxFile(cPfxFilename, cPfxPassword)
IF (nSuccess != 1)
    ? oCertStore:LastErrorText
    oCrypt:destroy()
    oCertStore:destroy()
    RETURN
ENDIF

oJsonCN := CreateObject("Chilkat.JsonObject")
oJsonCN:UpdateString("CN", cSigningCertSubject)

oCert := CreateObject("Chilkat.Cert")
nSuccess := oCertStore:FindCert(oJsonCN, oCert)
IF (nSuccess == 0)
    ? "Failed to find certificate by subject common name."
    oCrypt:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

//  Tell the crypt component to use this cert.
nSuccess := oCrypt:SetSigningCert(oCert)

//  We can sign any type of file, creating a .p7s as output:
cInFile := "/Users/chilkat/testData/pdf/sample.pdf"
cSigFile := "/Users/chilkat/testData/p7s/sample.p7s"

nSuccess := oCrypt:CreateP7S(cInFile, cSigFile)
IF (nSuccess == 0)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

nSuccess := oCrypt:VerifyP7S(cInFile, cSigFile)
IF (nSuccess == 0)
    ? oCrypt:LastErrorText
    oCrypt:destroy()
    oCertStore:destroy()
    oJsonCN:destroy()
    oCert:destroy()
    RETURN
ENDIF

? "Success!"

oCrypt:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()