Sample code for 30+ languages & platforms
Zig

Cape Verde Fatura Eletrónica XML Digital Signature

See more XML Digital Signatures Examples

Demonstrates how to sign XML to create a signed invoice compatible with the Cape Verde Fatura Eletrónica requirements.

Note: This example requires Chilkat v9.5.0.89 or greater for a small feature to provide the ability produce mult-line base64 signature ending with 

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // First create the following XML to be signed.  This can be created programmatically as shown in the code below, or
    // you can simply load the XML to be signed from a file..

    // <?xml version="1.0" encoding="utf-8"?>
    // <Dfe xmlns="urn:cv:efatura:xsd:v1.0" DocumentTypeCode="1" Id="CV2210818123456789000010100000000546735014601" Version="1.0">
    //     <Invoice>
    //         <LedCode>1</LedCode>
    //         <Serie>SL</Serie>
    //         <DocumentNumber>5</DocumentNumber>
    //         <IssueDate>2021-08-30</IssueDate>
    //         <IssueTime>00:01:00</IssueTime>
    //         <TaxPointDate>2021-08-18</TaxPointDate>
    //         <EmitterParty>
    //             <TaxId CountryCode="CV">123456789</TaxId>
    //             <Name>Maria</Name>
    //             <Address CountryCode="CV">
    //                 <State>Estado do Emissor</State>
    //                 <City>Cidade do Emissor</City>
    //                 <Region>Regi�o do Emissor</Region>
    //                 <Street>Rua do Emissor</Street>
    //                 <StreetDetail>Detalhe da rua do Emissor</StreetDetail>
    //                 <BuildingName>Pr�dio do Emissor</BuildingName>
    //                 <BuildingNumber>1</BuildingNumber>
    //                 <BuildingFloor>2</BuildingFloor>
    //                 <PostalCode>7600</PostalCode>
    //                 <AddressDetail>Detalhe de endere�o do Emissor</AddressDetail>
    //                 <AddressCode>CV774999999999999319</AddressCode>
    //             </Address>
    //             <Contacts>
    //                 <Telephone>1234567</Telephone>
    //                 <Mobilephone>1234568</Mobilephone>
    //                 <Telefax>1234569</Telefax>
    //                 <Email>somebody@example.com</Email>
    //                 <Website>emissor.efatura.cv</Website>
    //             </Contacts>
    //         </EmitterParty>
    //         <ReceiverParty>
    //             <TaxId CountryCode="CV">123456789</TaxId>
    //             <Name>Nome do Recetor</Name>
    //             <Address CountryCode="CV">
    //                 <State>Estado do Recetor</State>
    //                 <City>Cidade do Recetor</City>
    //                 <Region>Regi�o do Recetor</Region>
    //                 <Street>Rua do Recetor</Street>
    //                 <StreetDetail>Detalhe da rua do Recetor</StreetDetail>
    //                 <BuildingName>Pr�dio do Recetor</BuildingName>
    //                 <BuildingNumber>2</BuildingNumber>
    //                 <BuildingFloor>3</BuildingFloor>
    //                 <PostalCode>7601</PostalCode>
    //                 <AddressDetail>Detalhe de endere�o do Recetor</AddressDetail>
    //                 <AddressCode>CV774999999999999319</AddressCode>
    //             </Address>
    //             <Contacts>
    //                 <Telephone>2234567</Telephone>
    //                 <Mobilephone>2234568</Mobilephone>
    //                 <Telefax>2234569</Telefax>
    //                 <Email>rector@efatura.cv</Email>
    //                 <Website>rector.efatura.cv</Website>
    //             </Contacts>
    //         </ReceiverParty>
    //         <Lines>
    //             <Line LineTypeCode="N">
    //                 <Id>01</Id>
    //                 <OrderLineReference>1</OrderLineReference>
    //                 <Quantity UnitCode="EA">3.15</Quantity>
    //                 <Price>123456</Price>
    //                 <PriceExtension>388886</PriceExtension>
    //                 <Discount>15</Discount>
    //                 <NetTotal>252776</NetTotal>
    //                 <Tax TaxTypeCode="IVA">
    //                     <TaxPercentage>15</TaxPercentage>
    //                 </Tax>
    //                 <Item>
    //                     <Description>Telem�vel</Description>
    //                     <Name>Telem�vel</Name>
    //                     <BrandName>Apple</BrandName>
    //                     <ModelName>iPhone X</ModelName>
    //                     <EmitterIdentification>TLMV</EmitterIdentification>
    //                     <StandardIdentification>
    //                         <GTIN>12345</GTIN>
    //                     </StandardIdentification>
    //                     <HazardousRiskIndicator>false</HazardousRiskIndicator>
    //                 </Item>
    //             </Line>
    //         </Lines>
    //         <Totals>
    //             <PriceExtensionTotalAmount>388886</PriceExtensionTotalAmount>
    //             <ChargeTotalAmount>0</ChargeTotalAmount>
    //             <DiscountTotalAmount>136110</DiscountTotalAmount>
    //             <NetTotalAmount>252776</NetTotalAmount>
    //             <Discount ValueType="P">20</Discount>
    //             <TaxTotalAmount>37916</TaxTotalAmount>
    //             <PayableAmount>290692</PayableAmount>
    //         </Totals>
    //         <References>
    //             <Reference>
    //                 <FiscalDocument IsOldDocument="false">CV1200520123456789000112345678901112345678901</FiscalDocument>
    //                 <PaymentAmount>12345</PaymentAmount>
    //                 <Tax TaxTypeCode="IR">
    //                     <TaxPercentage>10</TaxPercentage>
    //                 </Tax>
    //             </Reference>
    //             <Reference>
    //                 <FiscalDocument IsOldDocument="false">CV1200520123456789000112345678901112345678903</FiscalDocument>
    //                 <PaymentAmount>98765</PaymentAmount>
    //                 <Tax TaxTypeCode="IVA">
    //                     <TaxPercentage>15</TaxPercentage>
    //                 </Tax>
    //             </Reference>
    //         </References>
    //         <Payments>
    //             <PaymentDueDate>2021-12-31</PaymentDueDate>
    //             <PaymentTerms>
    //                 <Note>Juros de 12% a partir da data de vencimento</Note>
    //             </PaymentTerms>
    //             <PayeeFinancialAccount>
    //                 <NIB>123456789012345678901</NIB>
    //                 <Name>Nome da Conta 1</Name>
    //             </PayeeFinancialAccount>
    //             <PayeeFinancialAccount>
    //                 <NIB>123456789012345678902</NIB>
    //                 <Name>Nome da Conta 2</Name>
    //             </PayeeFinancialAccount>
    //         </Payments>
    //         <Note>Texto livre</Note>
    //     </Invoice>
    //     <Transmission>
    //         <IssueMode>2</IssueMode>
    //         <TransmitterTaxId CountryCode="CV">123456789</TransmitterTaxId>
    //         <Software>
    //             <Code>EMIPUB</Code>
    //             <Name>DNRE Emissor P�blico</Name>
    //             <Version>Build #53 12-07-2021 14:00:47 CVT - master</Version>
    //         </Software>
    //         <Contingency>
    //             <LedCode>1</LedCode>
    //             <IssueDate>2020-01-01</IssueDate>
    //             <IssueTime>01:02:03</IssueTime>
    //             <ReasonTypeCode>1</ReasonTypeCode>
    //         </Contingency>
    //     </Transmission>
    //     <RepositoryCode>2</RepositoryCode>
    // </Dfe>

    // Create the XML to be signed...

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

    const xml_to_sign = try chilkat.Xml.init();
    defer xml_to_sign.deinit();
    xml_to_sign.setTag("Dfe");
    xml_to_sign.addAttribute("xmlns", "urn:cv:efatura:xsd:v1.0") catch {};
    xml_to_sign.addAttribute("DocumentTypeCode", "1") catch {};
    xml_to_sign.addAttribute("Id", "CV2210818123456789000010100000000546735014601") catch {};
    xml_to_sign.addAttribute("Version", "1.0") catch {};
    xml_to_sign.updateChildContent("Invoice|LedCode", "1");
    xml_to_sign.updateChildContent("Invoice|Serie", "SL");
    xml_to_sign.updateChildContent("Invoice|DocumentNumber", "5");
    xml_to_sign.updateChildContent("Invoice|IssueDate", "2021-08-30");
    xml_to_sign.updateChildContent("Invoice|IssueTime", "00:01:00");
    xml_to_sign.updateChildContent("Invoice|TaxPointDate", "2021-08-18");
    xml_to_sign.updateAttrAt("Invoice|EmitterParty|TaxId", true, "CountryCode", "CV") catch {};
    xml_to_sign.updateChildContent("Invoice|EmitterParty|TaxId", "123456789");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Name", "Maria");
    xml_to_sign.updateAttrAt("Invoice|EmitterParty|Address", true, "CountryCode", "CV") catch {};
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|State", "Estado do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|City", "Cidade do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|Region", "Regi�o do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|Street", "Rua do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|StreetDetail", "Detalhe da rua do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|BuildingName", "Pr�dio do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|BuildingNumber", "1");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|BuildingFloor", "2");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|PostalCode", "7600");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|AddressDetail", "Detalhe de endere�o do Emissor");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Address|AddressCode", "CV774999999999999319");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Contacts|Telephone", "1234567");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Contacts|Mobilephone", "1234568");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Contacts|Telefax", "1234569");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Contacts|Email", "somebody@example.com");
    xml_to_sign.updateChildContent("Invoice|EmitterParty|Contacts|Website", "emissor.efatura.cv");
    xml_to_sign.updateAttrAt("Invoice|ReceiverParty|TaxId", true, "CountryCode", "CV") catch {};
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|TaxId", "123456789");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Name", "Nome do Recetor");
    xml_to_sign.updateAttrAt("Invoice|ReceiverParty|Address", true, "CountryCode", "CV") catch {};
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|State", "Estado do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|City", "Cidade do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|Region", "Regi�o do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|Street", "Rua do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|StreetDetail", "Detalhe da rua do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|BuildingName", "Pr�dio do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|BuildingNumber", "2");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|BuildingFloor", "3");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|PostalCode", "7601");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|AddressDetail", "Detalhe de endere�o do Recetor");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Address|AddressCode", "CV774999999999999319");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Contacts|Telephone", "2234567");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Contacts|Mobilephone", "2234568");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Contacts|Telefax", "2234569");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Contacts|Email", "rector@efatura.cv");
    xml_to_sign.updateChildContent("Invoice|ReceiverParty|Contacts|Website", "rector.efatura.cv");
    xml_to_sign.updateAttrAt("Invoice|Lines|Line", true, "LineTypeCode", "N") catch {};
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Id", "01");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|OrderLineReference", "1");
    xml_to_sign.updateAttrAt("Invoice|Lines|Line|Quantity", true, "UnitCode", "EA") catch {};
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Quantity", "3.15");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Price", "123456");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|PriceExtension", "388886");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Discount", "15");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|NetTotal", "252776");
    xml_to_sign.updateAttrAt("Invoice|Lines|Line|Tax", true, "TaxTypeCode", "IVA") catch {};
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Tax|TaxPercentage", "15");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|Description", "Telem�vel");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|Name", "Telem�vel");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|BrandName", "Apple");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|ModelName", "iPhone X");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|EmitterIdentification", "TLMV");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|StandardIdentification|GTIN", "12345");
    xml_to_sign.updateChildContent("Invoice|Lines|Line|Item|HazardousRiskIndicator", "false");
    xml_to_sign.updateChildContent("Invoice|Totals|PriceExtensionTotalAmount", "388886");
    xml_to_sign.updateChildContent("Invoice|Totals|ChargeTotalAmount", "0");
    xml_to_sign.updateChildContent("Invoice|Totals|DiscountTotalAmount", "136110");
    xml_to_sign.updateChildContent("Invoice|Totals|NetTotalAmount", "252776");
    xml_to_sign.updateAttrAt("Invoice|Totals|Discount", true, "ValueType", "P") catch {};
    xml_to_sign.updateChildContent("Invoice|Totals|Discount", "20");
    xml_to_sign.updateChildContent("Invoice|Totals|TaxTotalAmount", "37916");
    xml_to_sign.updateChildContent("Invoice|Totals|PayableAmount", "290692");
    xml_to_sign.updateAttrAt("Invoice|References|Reference|FiscalDocument", true, "IsOldDocument", "false") catch {};
    xml_to_sign.updateChildContent("Invoice|References|Reference|FiscalDocument", "CV1200520123456789000112345678901112345678901");
    xml_to_sign.updateChildContent("Invoice|References|Reference|PaymentAmount", "12345");
    xml_to_sign.updateAttrAt("Invoice|References|Reference|Tax", true, "TaxTypeCode", "IR") catch {};
    xml_to_sign.updateChildContent("Invoice|References|Reference|Tax|TaxPercentage", "10");
    xml_to_sign.updateAttrAt("Invoice|References|Reference[1]|FiscalDocument", true, "IsOldDocument", "false") catch {};
    xml_to_sign.updateChildContent("Invoice|References|Reference[1]|FiscalDocument", "CV1200520123456789000112345678901112345678903");
    xml_to_sign.updateChildContent("Invoice|References|Reference[1]|PaymentAmount", "98765");
    xml_to_sign.updateAttrAt("Invoice|References|Reference[1]|Tax", true, "TaxTypeCode", "IVA") catch {};
    xml_to_sign.updateChildContent("Invoice|References|Reference[1]|Tax|TaxPercentage", "15");
    xml_to_sign.updateChildContent("Invoice|Payments|PaymentDueDate", "2021-12-31");
    xml_to_sign.updateChildContent("Invoice|Payments|PaymentTerms|Note", "Juros de 12% a partir da data de vencimento");
    xml_to_sign.updateChildContent("Invoice|Payments|PayeeFinancialAccount|NIB", "123456789012345678901");
    xml_to_sign.updateChildContent("Invoice|Payments|PayeeFinancialAccount|Name", "Nome da Conta 1");
    xml_to_sign.updateChildContent("Invoice|Payments|PayeeFinancialAccount[1]|NIB", "123456789012345678902");
    xml_to_sign.updateChildContent("Invoice|Payments|PayeeFinancialAccount[1]|Name", "Nome da Conta 2");
    xml_to_sign.updateChildContent("Invoice|Note", "Texto livre");
    xml_to_sign.updateChildContent("Transmission|IssueMode", "2");
    xml_to_sign.updateAttrAt("Transmission|TransmitterTaxId", true, "CountryCode", "CV") catch {};
    xml_to_sign.updateChildContent("Transmission|TransmitterTaxId", "123456789");
    xml_to_sign.updateChildContent("Transmission|Software|Code", "EMIPUB");
    xml_to_sign.updateChildContent("Transmission|Software|Name", "DNRE Emissor P�blico");
    xml_to_sign.updateChildContent("Transmission|Software|Version", "Build #53 12-07-2021 14:00:47 CVT - master");
    xml_to_sign.updateChildContent("Transmission|Contingency|LedCode", "1");
    xml_to_sign.updateChildContent("Transmission|Contingency|IssueDate", "2020-01-01");
    xml_to_sign.updateChildContent("Transmission|Contingency|IssueTime", "01:02:03");
    xml_to_sign.updateChildContent("Transmission|Contingency|ReasonTypeCode", "1");
    xml_to_sign.updateChildContent("RepositoryCode", "2");

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

    gen.setSigLocation("Dfe");
    gen.setSigLocationMod(0);
    gen.setSigId("xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac");
    gen.setSigNamespacePrefix("ds");
    gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
    gen.setSigValueId("xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-sigvalue");
    gen.setSignedInfoCanonAlg("C14N");
    gen.setSignedInfoDigestMethod("sha256");

    // Set the KeyInfoId before adding references..
    gen.setKeyInfoId("xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-keyinfo");

    // Create an Object to be added to the Signature.
    const object1 = try chilkat.Xml.init();
    defer object1.deinit();
    object1.setTag("xades:QualifyingProperties");
    object1.addAttribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#") catch {};
    object1.addAttribute("xmlns:xades141", "http://uri.etsi.org/01903/v1.4.1#") catch {};
    object1.addAttribute("Target", "#xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac") catch {};
    object1.updateAttrAt("xades:SignedProperties", true, "Id", "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-signedprops") 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:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", true, "ObjectReference", "#xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-ref0") catch {};
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", "DFE Signature");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", "efatura-uri");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:DocumentationReferences|xades:DocumentationReference", "https://efatura.cv");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "text/xml");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", "UTF-8");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Identifier", "http://uri.etsi.org/01903/v1.2.2#ProofOfApproval");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:CommitmentTypeId|xades:Description", "Indicates that the signer has approved the content of the signed data object");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication|xades:AllSignedDataObjects", "");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:CommitmentTypeId|xades:Identifier", "http://uri.etsi.org/01903/v1.2.2#ProofOfOrigin");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:CommitmentTypeId|xades:Description", "Indicates that the signer recognizes to have created, approved and sent the signed data object");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:ObjectReference", "#xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-ref0");
    object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:CommitmentTypeIndication[1]|xades:CommitmentTypeQualifiers|xades:CommitmentTypeQualifier", "CV-FE-123456789");

    gen.addObject("", try object1.getXml(alloc), "", "") catch {};

    // -------- Reference 1 --------
    gen.addSameDocRef("CV2210818123456789000010100000000546735014601", "sha256", "", "", "") catch {};
    gen.setRefIdAttr("CV2210818123456789000010100000000546735014601", "xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-ref0") catch {};

    // -------- Reference 2 --------
    gen.addSameDocRef("xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-keyinfo", "sha256", "C14N", "", "") catch {};

    // -------- Reference 3 --------
    gen.addObjectRef("xmldsig-cbd86598-e75e-4ae4-9a27-3b4ee2a789ac-signedprops", "sha256", "C14N", "", "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");
    gen.setX509Type("IssuerSerial,SubjectName,Certificate");

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

    // Note: The last keyword (Base64Cr13Entity) requires Chilkat v9.5.0.89 or later.
    gen.setBehaviors("IndentedSignature,Base64Cr13Entity,ForceAddEnvelopedSignatureTransform,Base64Cr13Entity");

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

    std.debug.print("{s}\n", .{try sb_xml.getAsString(alloc)});

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

    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", .{});
}