Sample code for 30+ languages & platforms
PureBasic

CAdES BES Attached (Opaque) Signature

See more Encryption Examples

Demonstrates how to create a CAdES BES attached signature file (.p7m). This is a PKCS7 signature format (known as an opaque, or "attached" signature) where the file data is embedded within the signature file. The signature verification both verifies the signature and extracts the original data.

(A "detached" signature is where the original data is NOT included within the PKCS7 signature format.)

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCrypt2.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

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

    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Use a digital certificate and private key from a PFX file (.pfx or .p12).
    pfxPath.s = "qa_data/pfx/myCertAndPrivateKey.p12"
    pfxPassword.s = "password"

    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,pfxPath,pfxPassword)
    If success <> 1
        Debug CkCert::ckLastErrorText(cert)
        CkCrypt2::ckDispose(crypt)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Tell the crypt component to use this cert.
    success = CkCrypt2::ckSetSigningCert(crypt,cert)
    If success <> 1
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkCrypt2::ckDispose(crypt)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; The CadesEnabled property applies to all methods that create PKCS7 signatures. 
    ; To create a CAdES-BES signature, set this property equal to true. 
    CkCrypt2::setCkCadesEnabled(crypt, 1)

    ; To sign with SHA1, set the HashAlgorithm property to "sha1"
    CkCrypt2::setCkHashAlgorithm(crypt, "sha1")

    ; To sign with SHA256, set the HashAlgorithm property to "SHA256".
    CkCrypt2::setCkHashAlgorithm(crypt, "sha256")

    ; Other hash algorithm choices are "md5", "md2", "sha384", and "sha512"

    ; We can sign any type of file, creating a .p7m as output.
    ; The .p7m contains the signature and also embeds the data of the file that is signed.
    inFile.s = "qa_data/json/sample.json"
    sigFile.s = "qa_output/signature.p7m"

    ; -----------------------------------------------------------------------------------------
    ; Also see Chilkat's online tool to examine a .p7m and generate code to duplicate the .p7m
    ; -----------------------------------------------------------------------------------------

    ; Create the CAdES-BES attached signature, which contains the original data.
    success = CkCrypt2::ckCreateP7M(crypt,inFile,sigFile)
    If success = 0
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkCrypt2::ckDispose(crypt)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Verify the .p7m file and extract the original file from the .p7m. 
    extractedToFilePath.s = "qa_output/sample.json"
    success = CkCrypt2::ckVerifyP7M(crypt,sigFile,extractedToFilePath)
    If success = 0
        Debug CkCrypt2::ckLastErrorText(crypt)
        CkCrypt2::ckDispose(crypt)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Success!"


    CkCrypt2::ckDispose(crypt)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure