Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    // 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>

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

    const gen = try chilkat.XmlDSigGen.init();
    defer gen.deinit();

    gen.setSigLocation("p:FatturaElettronica");
    gen.setSigLocationMod(0);
    gen.setSigId("signature-5580-7534-6530-8286");
    gen.addSignatureNamespace("xadesv1410", "http://uri.etsi.org/01903/v1.4.1") catch {};
    gen.addSignatureNamespace("xades", "http://uri.etsi.org/01903/v1.3.2#") catch {};
    // xmlRoot tag = p:FatturaElettronica
    // has xmlns:ds!
    gen.setSigValueId("signature-value-5957-3819-1360-0727");
    gen.setSignedInfoCanonAlg("EXCL_C14N");
    gen.setSignedInfoDigestMethod("sha256");

    // Set the KeyInfoId before adding references..
    gen.setKeyInfoId("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.

    const object1 = try chilkat.Xml.init();
    defer object1.deinit();
    object1.setTag("xades:QualifyingProperties");
    object1.addAttribute("Target", "#signature-5580-7534-6530-8286") catch {};
    object1.updateAttrAt("xades:SignedProperties", true, "Id", "signed-properties-1545-8800-2160-3000") catch {};
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT");
    object1.updateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256") catch {};
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT");
    object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", "TO BE GENERATED BY CHILKAT");
    object1.updateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp", true, "Id", "signature-timestamp-5561-8212-3316-5191") catch {};
    object1.updateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|ds:CanonicalizationMethod", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#") catch {};
    object1.updateAttrAt("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp", true, "Encoding", "http://uri.etsi.org/01903/v1.2.2#DER") catch {};
    object1.updateChildContent("xades:UnsignedProperties|xades:UnsignedSignatureProperties|xades:SignatureTimeStamp|xades:EncapsulatedTimeStamp", "TO BE GENERATED BY CHILKAT");

    gen.addObject("signature-object-8923-2359-1722-2161", try object1.getXml(alloc), "", "") catch {};

    // -------- 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>

    const xml1 = try chilkat.Xml.init();
    defer xml1.deinit();
    xml1.setTag("ds:Transforms");
    xml1.updateAttrAt("ds:Transform", true, "Algorithm", "http://www.w3.org/2002/06/xmldsig-filter2") catch {};
    xml1.updateAttrAt("ds:Transform|dsig-xpath:XPath", true, "xmlns:dsig-xpath", "http://www.w3.org/2002/06/xmldsig-filter2") catch {};
    xml1.updateAttrAt("ds:Transform|dsig-xpath:XPath", true, "Filter", "subtract") catch {};
    xml1.updateChildContent("ds:Transform|dsig-xpath:XPath", "/descendant::ds:Signature");
    xml1.updateAttrAt("ds:Transform[1]", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#") catch {};

    gen.addSameDocRef2("", "sha256", xml1, "") catch {};

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

    const xml2 = try chilkat.Xml.init();
    defer xml2.deinit();
    xml2.setTag("ds:Transforms");
    xml2.updateAttrAt("ds:Transform", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#") catch {};

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

    // Provide a certificate + private key. (PFX password is test123)
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    gen.setX509Cert(cert, true) catch {};

    gen.setKeyInfoType("X509Data+KeyValue");
    gen.setX509Type("Certificate");

    // Load XML to be signed...
    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    xml_to_sign.getXmlSb(sb_xml) catch {};

    gen.setBehaviors("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:
    // -------------------------------------------------------------------------------------------

    const json_tsa = try chilkat.JsonObject.init();
    defer json_tsa.deinit();
    json_tsa.updateString("timestampToken.tsaUrl", "http://timestamp.digicert.com") catch {};
    json_tsa.updateBool("timestampToken.requestTsaCert", true) catch {};
    gen.setTsa(json_tsa) catch {};

    // Sign the XML...
    gen.createXmlDSigSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try gen.getLastErrorText(alloc)});
        return;
    };

    // Save the signed XML to a file.
    try sb_xml.writeFile("qa_output/signedXml.xml", "utf-8", false);

    // ----------------------------------------
    // Verify the signatures we just produced...
    const verifier = try chilkat.XmlDSig.init();
    defer verifier.deinit();
    verifier.loadSignatureSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
        return;
    };

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

    const num_sigs = verifier.getNumSignatures();
    var verify_idx: i32 = 0;
    while (verify_idx < num_sigs) {
        verifier.setSelector(verify_idx);
        verifier.verifySignature(true) catch {
            std.debug.print("{s}\n", .{try verifier.getLastErrorText(alloc)});
            return;
        };

        verify_idx = verify_idx + 1;
    }

    std.debug.print("All signatures were successfully verified.\n", .{});
}