Sample code for 30+ languages & platforms
Unicode C++

XML-DSig Add EncapsulatedTimestamp when Signing

See more XML Digital Signatures Examples

Demonstrates how to add an EncapsulatedTimestamp at the time of creating an XML signature.

Note: This example requires Chilkat v9.5.0.90 or greater.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkXmlDSigW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    success = true;

    // Create the folllowing XML to be signed...

    // Use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    // <p:FatturaElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPR12" xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd">
    //     <FatturaElettronicaHeader>
    //         <DatiTrasmissione>
    //             <IdTrasmittente>
    //                 <IdPaese>IT</IdPaese>
    //                 <IdCodice>01234567890</IdCodice>
    //             </IdTrasmittente>
    //             <ProgressivoInvio>00001</ProgressivoInvio>
    //             <FormatoTrasmissione>FPR12</FormatoTrasmissione>
    //             <CodiceDestinatario>0000000</CodiceDestinatario>
    //             <PECDestinatario>betagamma@pec.it</PECDestinatario>
    //         </DatiTrasmissione>
    //         <CedentePrestatore>
    //             <DatiAnagrafici>
    //                 <IdFiscaleIVA>
    //                     <IdPaese>IT</IdPaese>
    //                     <IdCodice>01234567890</IdCodice>
    //                 </IdFiscaleIVA>
    //                 <Anagrafica>
    //                     <Denominazione>ALPHA SRL</Denominazione>
    //                 </Anagrafica>
    //                 <RegimeFiscale>RF19</RegimeFiscale>
    //             </DatiAnagrafici>
    //             <Sede>
    //                 <Indirizzo>VIALE ROMA 543</Indirizzo>
    //                 <CAP>07100</CAP>
    //                 <Comune>SASSARI</Comune>
    //                 <Provincia>SS</Provincia>
    //                 <Nazione>IT</Nazione>
    //             </Sede>
    //         </CedentePrestatore>
    //         <CessionarioCommittente>
    //             <DatiAnagrafici>
    //                 <CodiceFiscale>09876543210</CodiceFiscale>
    //                 <Anagrafica>
    //                     <Denominazione>AMMINISTRAZIONE BETA</Denominazione>
    //                 </Anagrafica>
    //             </DatiAnagrafici>
    //             <Sede>
    //                 <Indirizzo>VIA TORINO 38-B</Indirizzo>
    //                 <CAP>00145</CAP>
    // 
    //                 <Comune>ROMA</Comune>
    //                 <Provincia>RM</Provincia>
    //                 <Nazione>IT</Nazione>
    //             </Sede>
    //             <StabileOrganizzazione>
    //                 <Indirizzo>VIA CASELLE</Indirizzo>
    //                 <NumeroCivico>4/D</NumeroCivico>
    //                 <CAP>25027</CAP>
    //                 <Comune>QUINZANO D'OGLIO</Comune>
    //                 <Provincia>BS</Provincia>
    //                 <Nazione>IT</Nazione>
    //             </StabileOrganizzazione>
    //             <RappresentanteFiscale>
    //                 <IdFiscaleIVA>
    //                     <IdPaese>DE</IdPaese>
    //                     <IdCodice>DE12345</IdCodice>
    //                 </IdFiscaleIVA>
    //                 <Denominazione>RFCC - DENOMINAZIONE</Denominazione>
    //             </RappresentanteFiscale>
    //         </CessionarioCommittente>
    //     </FatturaElettronicaHeader>
    //     <FatturaElettronicaBody>
    //         <DatiGenerali>
    //             <DatiGeneraliDocumento>
    //                 <TipoDocumento>TD01</TipoDocumento>
    //                 <Divisa>EUR</Divisa>
    //                 <Data>2022-03-03</Data>
    //                 <Numero>123</Numero>
    //                 <Causale>LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA</Causale>
    //                 <Causale>SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB</Causale>
    //             </DatiGeneraliDocumento>
    //             <DatiOrdineAcquisto>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>66685</IdDocumento>
    //                 <NumItem>1</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiOrdineAcquisto>
    //             <DatiContratto>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>123</IdDocumento>
    //                 <Data>2022-01-01</Data>
    //                 <NumItem>5</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiContratto>
    //             <DatiConvenzione>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>456</IdDocumento>
    //                 <NumItem>5</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiConvenzione>
    //             <DatiRicezione>
    //                 <RiferimentoNumeroLinea>1</RiferimentoNumeroLinea>
    //                 <IdDocumento>789</IdDocumento>
    //                 <NumItem>5</NumItem>
    //                 <CodiceCUP>123abc</CodiceCUP>
    //                 <CodiceCIG>456def</CodiceCIG>
    //             </DatiRicezione>
    //             <DatiTrasporto>
    //                 <DatiAnagraficiVettore>
    //                     <IdFiscaleIVA>
    //                         <IdPaese>IT</IdPaese>
    //                         <IdCodice>24681012141</IdCodice>
    //                     </IdFiscaleIVA>
    //                     <Anagrafica>
    //                         <Denominazione>Trasporto spa</Denominazione>
    //                     </Anagrafica>
    //                 </DatiAnagraficiVettore>
    //                 <DataOraConsegna>2022-03-01T14:26:39</DataOraConsegna>
    //             </DatiTrasporto>
    //         </DatiGenerali>
    //         <DatiBeniServizi>
    //             <DettaglioLinee>
    //                 <NumeroLinea>1</NumeroLinea>
    //                 <Descrizione>DESCRIZIONE DELLA FORNITURA</Descrizione>
    //                 <Quantita>5.005</Quantita>
    //                 <PrezzoUnitario>1.00</PrezzoUnitario>
    //                 <PrezzoTotale>5.00</PrezzoTotale>
    //                 <AliquotaIVA>22.00</AliquotaIVA>
    //             </DettaglioLinee>
    //             <DatiRiepilogo>
    //                 <AliquotaIVA>22.00</AliquotaIVA>
    //                 <ImponibileImporto>5.00</ImponibileImporto>
    //                 <Imposta>1.10</Imposta>
    //                 <EsigibilitaIVA>I</EsigibilitaIVA>
    //             </DatiRiepilogo>
    //         </DatiBeniServizi>
    //         <DatiPagamento>
    //             <CondizioniPagamento>TP01</CondizioniPagamento>
    //             <DettaglioPagamento>
    //                 <ModalitaPagamento>MP01</ModalitaPagamento>
    //                 <DataScadenzaPagamento>2017-02-18</DataScadenzaPagamento>
    //                 <ImportoPagamento>6.10</ImportoPagamento>
    //             </DettaglioPagamento>
    //         </DatiPagamento>
    //     </FatturaElettronicaBody>
    // </p:FatturaElettronica>

    CkXmlW xmlToSign;
    xmlToSign.put_Tag(L"p:FatturaElettronica");
    xmlToSign.AddAttribute(L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
    xmlToSign.AddAttribute(L"xmlns:p",L"http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2");
    xmlToSign.AddAttribute(L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    xmlToSign.AddAttribute(L"versione",L"FPR12");
    xmlToSign.AddAttribute(L"xsi:schemaLocation",L"http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 http://www.fatturapa.gov.it/export/fatturazione/sdi/fatturapa/v1.2/Schema_del_file_xml_FatturaPA_versione_1.2.xsd");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdPaese",L"IT");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|DatiTrasmissione|IdTrasmittente|IdCodice",L"01234567890");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|DatiTrasmissione|ProgressivoInvio",L"00001");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|DatiTrasmissione|FormatoTrasmissione",L"FPR12");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|DatiTrasmissione|CodiceDestinatario",L"0000000");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|DatiTrasmissione|PECDestinatario",L"betagamma@pec.it");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdPaese",L"IT");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|IdFiscaleIVA|IdCodice",L"01234567890");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|Anagrafica|Denominazione",L"ALPHA SRL");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|DatiAnagrafici|RegimeFiscale",L"RF19");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|Sede|Indirizzo",L"VIALE ROMA 543");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|Sede|CAP",L"07100");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|Sede|Comune",L"SASSARI");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|Sede|Provincia",L"SS");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CedentePrestatore|Sede|Nazione",L"IT");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|CodiceFiscale",L"09876543210");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|DatiAnagrafici|Anagrafica|Denominazione",L"AMMINISTRAZIONE BETA");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Indirizzo",L"VIA TORINO 38-B");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|Sede|CAP",L"00145");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Comune",L"ROMA");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Provincia",L"RM");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|Sede|Nazione",L"IT");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Indirizzo",L"VIA CASELLE");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|NumeroCivico",L"4/D");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|CAP",L"25027");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Comune",L"QUINZANO D'OGLIO");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Provincia",L"BS");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|StabileOrganizzazione|Nazione",L"IT");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdPaese",L"DE");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|IdFiscaleIVA|IdCodice",L"DE12345");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaHeader|CessionarioCommittente|RappresentanteFiscale|Denominazione",L"RFCC - DENOMINAZIONE");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|TipoDocumento",L"TD01");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Divisa",L"EUR");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Data",L"2022-03-03");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Numero",L"123");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale",L"LA FATTURA FA RIFERIMENTO AD UNA OPERAZIONE AAAA");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiGeneraliDocumento|Causale[1]",L"SEGUE DESCRIZIONE CAUSALE NEL CASO IN CUI NON SIANO STATI SUFFICIENTI 200 CARATTERI AAAAAAAAAAA BBBBBBBBBBBBBBBBB");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|RiferimentoNumeroLinea",L"1");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|IdDocumento",L"66685");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|NumItem",L"1");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCUP",L"123abc");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiOrdineAcquisto|CodiceCIG",L"456def");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiContratto|RiferimentoNumeroLinea",L"1");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiContratto|IdDocumento",L"123");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiContratto|Data",L"2022-01-01");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiContratto|NumItem",L"5");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCUP",L"123abc");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiContratto|CodiceCIG",L"456def");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|RiferimentoNumeroLinea",L"1");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|IdDocumento",L"456");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|NumItem",L"5");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCUP",L"123abc");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiConvenzione|CodiceCIG",L"456def");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|RiferimentoNumeroLinea",L"1");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|IdDocumento",L"789");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|NumItem",L"5");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCUP",L"123abc");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiRicezione|CodiceCIG",L"456def");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdPaese",L"IT");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|IdFiscaleIVA|IdCodice",L"24681012141");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DatiAnagraficiVettore|Anagrafica|Denominazione",L"Trasporto spa");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiGenerali|DatiTrasporto|DataOraConsegna",L"2022-03-01T14:26:39");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|NumeroLinea",L"1");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Descrizione",L"DESCRIZIONE DELLA FORNITURA");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|Quantita",L"5.005");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoUnitario",L"1.00");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|PrezzoTotale",L"5.00");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DettaglioLinee|AliquotaIVA",L"22.00");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|AliquotaIVA",L"22.00");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|ImponibileImporto",L"5.00");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|Imposta",L"1.10");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiBeniServizi|DatiRiepilogo|EsigibilitaIVA",L"I");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiPagamento|CondizioniPagamento",L"TP01");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ModalitaPagamento",L"MP01");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|DataScadenzaPagamento",L"2017-02-18");
    xmlToSign.UpdateChildContent(L"FatturaElettronicaBody|DatiPagamento|DettaglioPagamento|ImportoPagamento",L"6.10");

    CkXmlDSigGenW gen;

    gen.put_SigLocation(L"p:FatturaElettronica");
    gen.put_SigLocationMod(0);
    gen.put_SigId(L"signature-5580-7534-6530-8286");
    gen.AddSignatureNamespace(L"xadesv1410",L"http://uri.etsi.org/01903/v1.4.1");
    gen.AddSignatureNamespace(L"xades",L"http://uri.etsi.org/01903/v1.3.2#");
    // xmlRoot tag = p:FatturaElettronica
    // has xmlns:ds!
    gen.put_SigValueId(L"signature-value-5957-3819-1360-0727");
    gen.put_SignedInfoCanonAlg(L"EXCL_C14N");
    gen.put_SignedInfoDigestMethod(L"sha256");

    // Set the KeyInfoId before adding references..
    gen.put_KeyInfoId(L"key-info-7422-1087-7530-8569");

    // Create the following signed properties object to be added to the signature:

    // Use this online tool to generate code from sample XML: 
    // Generate Code to Create XML

    // <xades:QualifyingProperties Target="#signature-5580-7534-6530-8286">
    //     <xades:SignedProperties Id="signed-properties-1545-8800-2160-3000">
    //         <xades:SignedSignatureProperties>
    //             <xades:SigningTime>TO BE GENERATED BY CHILKAT</xades:SigningTime>
    //             <xades:SigningCertificate>
    //                 <xades:Cert>
    //                     <xades:CertDigest>
    //                         <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <ds:DigestValue>TO BE GENERATED BY CHILKAT</ds:DigestValue>
    // 
    //                     </xades:CertDigest>
    //                     <xades:IssuerSerial>
    //                         <ds:X509IssuerName>TO BE GENERATED BY CHILKAT</ds:X509IssuerName>
    //                         <ds:X509SerialNumber>TO BE GENERATED BY CHILKAT</ds:X509SerialNumber>
    //                     </xades:IssuerSerial>
    //                 </xades:Cert>
    //             </xades:SigningCertificate>
    //         </xades:SignedSignatureProperties>
    //     </xades:SignedProperties>
    //     <xades:UnsignedProperties>
    //         <xades:UnsignedSignatureProperties>
    //             <xades:SignatureTimeStamp Id="signature-timestamp-5561-8212-3316-5191">
    //                 <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                 <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp>
    //             </xades:SignatureTimeStamp>
    //         </xades:UnsignedSignatureProperties>
    //     </xades:UnsignedProperties>
    // </xades:QualifyingProperties>

    // Note: Chilkat will automatically fill in the values marked as "TO BE GENERATED BY CHILKAT" at the time of signing.
    // The EncapsulatedTimestamp will be automatically generated.

    CkXmlW object1;
    object1.put_Tag(L"xades:QualifyingProperties");
    object1.AddAttribute(L"Target",L"#signature-5580-7534-6530-8286");
    object1.UpdateAttrAt(L"xades:SignedProperties",true,L"Id",L"signed-properties-1545-8800-2160-3000");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateAttrAt(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod",true,L"Algorithm",L"http://www.w3.org/2001/04/xmlenc#sha256");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateChildContent(L"xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2",L"TO BE GENERATED BY CHILKAT");
    object1.UpdateAttrAt(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp",true,L"Id",L"signature-timestamp-5561-8212-3316-5191");
    object1.UpdateAttrAt(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");
    object1.UpdateAttrAt(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",true,L"Encoding",L"http://uri.etsi.org/01903/v1.2.2#DER");
    object1.UpdateChildContent(L"xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp",L"TO BE GENERATED BY CHILKAT");

    gen.AddObject(L"signature-object-8923-2359-1722-2161",object1.getXml(),L"",L"");

    // -------- Reference 1 --------
    // <ds:Transforms>
    //     <ds:Transform Algorithm="http://www.w3.org/2002/06/xmldsig-filter2">
    //         <dsig-xpath:XPath xmlns:dsig-xpath="http://www.w3.org/2002/06/xmldsig-filter2" Filter="subtract">/descendant::ds:Signature</dsig-xpath:XPath>
    //     </ds:Transform>
    //     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    // </ds:Transforms>

    CkXmlW xml1;
    xml1.put_Tag(L"ds:Transforms");
    xml1.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/2002/06/xmldsig-filter2");
    xml1.UpdateAttrAt(L"ds:Transform|dsig-xpath:XPath",true,L"xmlns:dsig-xpath",L"http://www.w3.org/2002/06/xmldsig-filter2");
    xml1.UpdateAttrAt(L"ds:Transform|dsig-xpath:XPath",true,L"Filter",L"subtract");
    xml1.UpdateChildContent(L"ds:Transform|dsig-xpath:XPath",L"/descendant::ds:Signature");
    xml1.UpdateAttrAt(L"ds:Transform[1]",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    gen.AddSameDocRef2(L"",L"sha256",xml1,L"");

    // -------- Reference 2 --------
    // <ds:Transforms>
    //     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    // </ds:Transforms>

    CkXmlW xml2;
    xml2.put_Tag(L"ds:Transforms");
    xml2.UpdateAttrAt(L"ds:Transform",true,L"Algorithm",L"http://www.w3.org/2001/10/xml-exc-c14n#");

    gen.AddObjectRef2(L"signed-properties-1545-8800-2160-3000",L"sha256",xml2,L"http://uri.etsi.org/01903#SignedProperties");

    // Provide a certificate + private key. (PFX password is test123)
    CkCertW cert;
    success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success != true) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    gen.SetX509Cert(cert,true);

    gen.put_KeyInfoType(L"X509Data+KeyValue");
    gen.put_X509Type(L"Certificate");

    // Load XML to be signed...
    CkStringBuilderW sbXml;
    xmlToSign.GetXmlSb(sbXml);

    gen.put_Behaviors(L"IndentedSignature,OmitAlreadyDefinedSigNamespace");

    // -------------------------------------------------------------------------------------------
    // To have the EncapsulatedTimeStamp automatically added, we only need to do 2 things.
    // 1) Add the <xades:EncapsulatedTimeStamp Encoding="http://uri.etsi.org/01903/v1.2.2#DER">TO BE GENERATED BY CHILKAT</xades:EncapsulatedTimeStamp>
    //    to the unsigned properties.
    // 2) Specify the TSA URL (Timestamping Authority URL).
    //    Here we specify the TSA URL:
    // -------------------------------------------------------------------------------------------

    CkJsonObjectW jsonTsa;
    jsonTsa.UpdateString(L"timestampToken.tsaUrl",L"http://timestamp.digicert.com");
    jsonTsa.UpdateBool(L"timestampToken.requestTsaCert",true);
    gen.SetTsa(jsonTsa);

    // Sign the XML...
    success = gen.CreateXmlDSigSb(sbXml);
    if (success != true) {
        wprintf(L"%s\n",gen.lastErrorText());
        return;
    }

    // Save the signed XML to a file.
    success = sbXml.WriteFile(L"qa_output/signedXml.xml",L"utf-8",false);

    // ----------------------------------------
    // Verify the signatures we just produced...
    CkXmlDSigW verifier;
    success = verifier.LoadSignatureSb(sbXml);
    if (success != true) {
        wprintf(L"%s\n",verifier.lastErrorText());
        return;
    }

    // Add "VerifyEncapsulatedTimeStamp" to the UncommonOptions to also verify any EncapsulatedTimeStamps
    verifier.put_UncommonOptions(L"VerifyEncapsulatedTimeStamp");

    int numSigs = verifier.get_NumSignatures();
    int verifyIdx = 0;
    while (verifyIdx < numSigs) {
        verifier.put_Selector(verifyIdx);
        bool verified = verifier.VerifySignature(true);
        if (verified != true) {
            wprintf(L"%s\n",verifier.lastErrorText());
            return;
        }

        verifyIdx = verifyIdx + 1;
    }

    wprintf(L"All signatures were successfully verified.\n");
    }