Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Create XAdES-EPES Factura Electrónica Signature

See more XAdES Examples

Demonstrates how to create an XAdES-EPES signature. The sample data is for the Costa Rica Factura Electrónica (http://www.hacienda.go.cr/contenido/14350-factura-electronica)

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

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Xml,
  Chilkat.StringBuilder,
  Chilkat.XmlDSigGen,
  Chilkat.Cert;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  sbXml: TStringBuilder;
  gen: TXmlDSigGen;
  xml: TXml;
  signedPropsId: string;
  keyInfoId: string;

begin
  success := False;

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

  //  This example will add an XAdES-EPES signature to the file
  //  at FacturaElectronica_cr.xml
  //  
  //  The XML to be signed looks like this:

  //  	<?xml version="1.0" encoding="UTF-8"?>
  //  	<FacturaElectronica xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/facturaElectronica" 
  //                 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  //  	   <Clave>70609022800030259092400100001010000000005100001701</Clave>
  //  	   <NumeroConsecutivo>00100001010000000007</NumeroConsecutivo>
  //  	   <FechaEmision>2018-02-08T22:05:20.3925782-06:00</FechaEmision>
  //  	   <Emisor>
  //  	   ...
  //  	   </Emisor>
  //  	   ...
  //  	   <Normativa>
  //  	      <NumeroResolucion>DGT-R-48-2016</NumeroResolucion>
  //  	      <FechaResolucion>20-02-2017 13:22:22</FechaResolucion>
  //  	   </Normativa>
  //  	</FacturaElectronica>

  //  First load a certificate w/ private key from a .pfx.
  //  (There are many different ways to provide the certificate + private key for signing.
  //  loading a .pfx is just one particular way of doing it..)

  cert := TCert.Create;
  success := cert.LoadPfxFile('qa_data/pfx/cert_test123.pfx','test123');
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Load XML to be signed.
  sbXml := TStringBuilder.Create;
  success := sbXml.LoadFile('qa_data/xades/factura_cr/FacturaElectronica.xml','utf-8');
  if (success <> True) then
    begin
      WriteLn('Failed to load file.');
      Exit;
    end;

  gen := TXmlDSigGen.Create;

  //  Indicate where the signature is to be placed.
  gen.SigLocation := 'FacturaElectronica';
  //  The SigId can be any random unique string in any format (in other words, there is no expected format).  
  //  For example, "Hello_23946359203502" or "A3982BF7130C".  It is arbitrary.
  //  However, other parts of the XML will reference the ID you use here.  (Search for the same string below and you'll see...)
  gen.SigId := 'Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0';
  gen.SigNamespacePrefix := 'ds';
  gen.SignedInfoCanonAlg := 'C14N';
  gen.SignedInfoDigestMethod := 'sha256';

  //  --------------------------------------------------------------------
  //  We want to build the QualifyingProperties XML, as shown below:
  //  You can generate the code that creates this XML using the online tool 
  //  at XML Code Generator

  //  I've place a copy of the QualifyingProperties XML at
  //  QualifyingProperties_cr.xml

  //           <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="QualifyingProperties-aa262416-8607-4f02-8897-0a6440a1ae03" Target="#Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //              <xades:SignedProperties Id="SignedProperties-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //                 <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:SignaturePolicyIdentifier>
  //                       <xades:SignaturePolicyId>
  //                          <xades:SigPolicyId>
  //                             <xades:Identifier>https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/Resolucion%20Comprobantes%20Electronicos%20%20DGT-R-48-2016.pdf</xades:Identifier>
  //                             <xades:Description />
  //                          </xades:SigPolicyId>
  //                          <xades:SigPolicyHash>
  //                             <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  //                             <ds:DigestValue>NmI5Njk1ZThkNzI0MmIzMGJmZDAyNDc4YjUwNzkzODM2NTBiOWUxNTBkMmI2YjgzYzZjM2I5NTZlNDQ4OWQzMQ==</ds:DigestValue>
  //                          </xades:SigPolicyHash>
  //                       </xades:SignaturePolicyId>
  //                    </xades:SignaturePolicyIdentifier>
  //                 </xades:SignedSignatureProperties>
  //                 <xades:SignedDataObjectProperties>
  //                    <xades:DataObjectFormat ObjectReference="#Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f">
  //                       <xades:MimeType>text/xml</xades:MimeType>
  //                       <xades:Encoding>UTF-8</xades:Encoding>
  //                    </xades:DataObjectFormat>
  //                 </xades:SignedDataObjectProperties>
  //              </xades:SignedProperties>
  //           </xades:QualifyingProperties>
  //  

  //  This is the code generated by the online tool to create the above XML.
  //  Note: Chilkat will automatically populate the strings indicated by "TO BE GENERATED BY CHILKAT" with actual/correct values
  //  when the XML is signed.
  xml := TXml.Create;
  xml.Tag := 'xades:QualifyingProperties';
  xml.AddAttribute('xmlns:xades','http://uri.etsi.org/01903/v1.3.2#');
  xml.AddAttribute('Id','QualifyingProperties-aa262416-8607-4f02-8897-0a6440a1ae03');
  xml.AddAttribute('Target','#Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0');
  xml.UpdateAttrAt('xades:SignedProperties',True,'Id','SignedProperties-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime','TO BE GENERATED BY CHILKAT');
  xml.UpdateAttrAt('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod',True,'Algorithm','http://www.w3.org/2001/04/xmlenc#sha256');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue','TO BE GENERATED BY CHILKAT');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2','TO BE GENERATED BY CHILKAT');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyId|xades:Identifier','https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/Resolucion%20Comprobantes%20Electronicos%20%20DGT-R-48-2016.pdf');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyId|xades:Description','');
  xml.UpdateAttrAt('xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyHash|ds:DigestMethod',True,'Algorithm','http://www.w3.org/2001/04/xmlenc#sha256');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyHash|ds:DigestValue','NmI5Njk1ZThkNzI0MmIzMGJmZDAyNDc4YjUwNzkzODM2NTBiOWUxNTBkMmI2YjgzYzZjM2I5NTZlNDQ4OWQzMQ==');
  xml.UpdateAttrAt('xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat',True,'ObjectReference','#Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType','text/xml');
  xml.UpdateChildContent('xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding','UTF-8');

  //  --------------------------------------------------------------------
  //  Add the QualifyingProperties as an Object in the Signature that is to be produced.

  gen.AddObject('XadesObjectId-674a431e-692c-4e0a-9d82-6275c85c5876',xml.GetXml(),'','');

  //  Add a Reference to the SignedProperties.
  signedPropsId := 'SignedProperties-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0';
  gen.AddObjectRef(signedPropsId,'sha256','EXCL_C14N','','http://uri.etsi.org/01903#SignedProperties');

  //  When the Signature is generated, the Reference to the SignedProperties will look like this:

  //           <ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#SignedProperties-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //              <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  //              <ds:DigestValue>GLOA1AWNZhJM9NlK515TJmCE+/EKHlrIQJkkyTPPL4E=</ds:DigestValue>
  //           </ds:Reference>

  //  --------------------------------------------------------------------
  //  Add a reference to the KeyInfo, which does not yet exist because it will be
  //  generated as part of the Signature.  However, we can specify the KeyInfo Id,
  //  and then specify that during signature generation, a reference to the KeyInfo (using the Id)
  //  should be added.

  //  We'll want a KeyInfo that will look like this:

  //           <ds:Reference Id="ReferenceKeyInfo" URI="#KeyInfoId-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //              <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  //              <ds:DigestValue>hbYK/DyNGpK4HtUQV8xsljxbrTJY4AS0SYOa1oW/FQw=</ds:DigestValue>
  //           </ds:Reference>

  keyInfoId := 'KeyInfoId-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0';
  gen.KeyInfoId := keyInfoId;
  gen.AddSameDocRef(keyInfoId,'sha256','EXCL_C14N','','');
  gen.SetRefIdAttr(keyInfoId,'ReferenceKeyInfo');

  //  --------------------------------------------------------------------
  //  Add our 3rd and final Reference, which is to the root element of the XML document being signed.

  gen.AddSameDocRef('','sha256','EXCL_C14N','','');
  gen.SetRefIdAttr('','Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f');

  //  The 3rd Reference will look like this:

  //           <ds:Reference Id="Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f" URI="">
  //              <ds:Transforms>
  //                 <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
  //              </ds:Transforms>
  //              <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  //              <ds:DigestValue>BvbWtgU5sBzQpS4xHOocHHmj97o/M895BsYb0ce+3LA=</ds:DigestValue>
  //           </ds:Reference>

  //  ----------------------------------------------------------------
  //  Provide the KeyInfo information.
  gen.KeyInfoType := 'X509Data';
  gen.X509Type := 'Certificate';
  success := gen.SetX509Cert(cert,True);
  if (success = False) then
    begin
      WriteLn(gen.LastErrorText);
      Exit;
    end;

  //  --------------------------------------------------------------------
  //  Create the XAdES-EPES signed XML.
  gen.Behaviors := 'IndentedSignature';

  success := gen.CreateXmlDSigSb(sbXml);
  if (success = False) then
    begin
      WriteLn(gen.LastErrorText);
      Exit;
    end;

  //  Examine the signed XML
  WriteLn(sbXml.GetAsString());

  //  The following XAdES-EPES signed XML is produced:

  //  <?xml version="1.0" encoding="UTF-8"?>
  //  <FacturaElectronica xmlns="https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/facturaElectronica" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  //     <Clave>70609022800030259092400100001010000000005100001701</Clave>
  //     <NumeroConsecutivo>00100001010000000007</NumeroConsecutivo>
  //     <FechaEmision>2018-02-08T22:05:20.3925782-06:00</FechaEmision>
  //     <Emisor>
  //        <Nombre>Xavier Zarpio Yonge</Nombre>
  //        <Identificacion>
  //           <Tipo>01</Tipo>
  //           <Numero>302590926</Numero>
  //        </Identificacion>
  //        <NombreComercial />
  //        <Ubicacion>
  //           <Provincia>3</Provincia>
  //           <Canton>01</Canton>
  //           <Distrito>01</Distrito>
  //           <Barrio>01</Barrio>
  //           <OtrasSenas>555 al sur de la plaza los Angeles.</OtrasSenas>
  //        </Ubicacion>
  //        <Telefono>
  //           <CodigoPais>506</CodigoPais>
  //           <NumTelefono>99929567</NumTelefono>
  //        </Telefono>
  //        <Fax>
  //           <CodigoPais>506</CodigoPais>
  //           <NumTelefono>99929567</NumTelefono>
  //        </Fax>
  //        <CorreoElectronico>xavieryarpio@gmail.com</CorreoElectronico>
  //     </Emisor>
  //     <Receptor>
  //        <Nombre>Andy Malamar Catal�n</Nombre>
  //        <Identificacion>
  //           <Tipo>01</Tipo>
  //           <Numero>305580724</Numero>
  //        </Identificacion>
  //        <NombreComercial />
  //        <Ubicacion>
  //           <Provincia>3</Provincia>
  //           <Canton>01</Canton>
  //           <Distrito>01</Distrito>
  //           <Barrio>01</Barrio>
  //           <OtrasSenas>Cartago, los �ngeles</OtrasSenas>
  //        </Ubicacion>
  //        <Telefono>
  //           <CodigoPais>506</CodigoPais>
  //           <NumTelefono>84431131</NumTelefono>
  //        </Telefono>
  //        <Fax>
  //           <CodigoPais>506</CodigoPais>
  //           <NumTelefono>00000000</NumTelefono>
  //        </Fax>
  //        <CorreoElectronico>andycatalan12@hotmail.es</CorreoElectronico>
  //     </Receptor>
  //     <CondicionVenta>01</CondicionVenta>
  //     <PlazoCredito>0</PlazoCredito>
  //     <MedioPago>01</MedioPago>
  //     <DetalleServicio>
  //        <LineaDetalle>
  //           <NumeroLinea>1</NumeroLinea>
  //           <Codigo>
  //              <Tipo>01</Tipo>
  //              <Codigo>11</Codigo>
  //           </Codigo>
  //           <Cantidad>1.000</Cantidad>
  //           <UnidadMedida>Unid</UnidadMedida>
  //           <UnidadMedidaComercial />
  //           <Detalle>servicios profesionales</Detalle>
  //           <PrecioUnitario>30000.00000</PrecioUnitario>
  //           <MontoTotal>30000.00000</MontoTotal>
  //           <NaturalezaDescuento />
  //           <SubTotal>30000.00000</SubTotal>
  //           <MontoTotalLinea>30000.00000</MontoTotalLinea>
  //        </LineaDetalle>
  //     </DetalleServicio>
  //     <ResumenFactura>
  //        <CodigoMoneda>CRC</CodigoMoneda>
  //        <TipoCambio>1.00000</TipoCambio>
  //        <TotalServGravados>0.00000</TotalServGravados>
  //        <TotalServExentos>30000.00000</TotalServExentos>
  //        <TotalMercanciasGravadas>0.00000</TotalMercanciasGravadas>
  //        <TotalMercanciasExentas>0.00000</TotalMercanciasExentas>
  //        <TotalGravado>0.00000</TotalGravado>
  //        <TotalExento>30000.00000</TotalExento>
  //        <TotalVenta>30000.00000</TotalVenta>
  //        <TotalDescuentos>0.00000</TotalDescuentos>
  //        <TotalVentaNeta>30000.00000</TotalVentaNeta>
  //        <TotalImpuesto>0.00000</TotalImpuesto>
  //        <TotalComprobante>30000.00000</TotalComprobante>
  //     </ResumenFactura>
  //     <Normativa>
  //        <NumeroResolucion>DGT-R-48-2016</NumeroResolucion>
  //        <FechaResolucion>20-02-2017 13:22:22</FechaResolucion>
  //     </Normativa>
  //  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //    <ds:SignedInfo>
  //      <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
  //      <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
  //      <ds:Reference URI="#SignedProperties-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0" Type="http://uri.etsi.org/01903#SignedProperties">
  //        <ds:Transforms>
  //          <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
  //        </ds:Transforms>
  //        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
  //        <ds:DigestValue>rk9bKozGf7gos5+gk77yd+6Or71uMtLKanLlJQ1CCJ4=</ds:DigestValue>
  //      </ds:Reference>
  //      <ds:Reference Id="Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f" URI="#KeyInfoId-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //        <ds:Transforms>
  //          <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
  //        </ds:Transforms>
  //        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
  //        <ds:DigestValue>AHAsUXqwwBjpkiSelrzz7TJ7WjbM3IWOOF210YuYSEU=</ds:DigestValue>
  //      </ds:Reference>
  //      <ds:Reference Id="Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f" URI="">
  //        <ds:Transforms>
  //          <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
  //          <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
  //        </ds:Transforms>
  //        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
  //        <ds:DigestValue>sU/yIR2Wh8xEaMQswFjQv65VCCm/1m/0Q+WZoORItw8=</ds:DigestValue>
  //      </ds:Reference>
  //    </ds:SignedInfo>
  //    <ds:SignatureValue>XL2OfTTE8mOwfF3jzZ9A+21xKImA6ClpahXwA7twJ4O6Zw4zLd9M4WIFOEd288Uch+Uvd3ixz+zn+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1s9qQM9esp3FRlACVeb1Qlytr4vgc5FlCqn0rMtjlF4=</ds:X509Certificate>
  //      </ds:X509Data>
  //    </ds:KeyInfo>
  //    <ds:Object Id="XadesObjectId-674a431e-692c-4e0a-9d82-6275c85c5876"><xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="QualifyingProperties-aa262416-8607-4f02-8897-0a6440a1ae03" Target="#Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //      <xades:SignedProperties Id="SignedProperties-Signature-d1cbfe99-fd8e-4e0f-b0b7-fc5bfe2f1dd0">
  //          <xades:SignedSignatureProperties>
  //              <xades:SigningTime>2018-06-19T18:44:02-0500</xades:SigningTime>
  //              <xades:SigningCertificate>
  //                  <xades:Cert>
  //                      <xades:CertDigest>
  //                          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  //                          <ds:DigestValue>b8Dptsa8ynvygBZYOLl2u6Nxn1RfzJlR44OmxEw+9ak=</ds:DigestValue>
  //                      </xades:CertDigest>
  //                      <xades:IssuerSerial>
  //                          <ds:X509IssuerName>C=GB, ST=Greater Manchester, L=Salford, O=COMODO CA Limited, CN=COMODO RSA Client Authentication and Secure Email CA</ds:X509IssuerName>
  //                          <ds:X509SerialNumber>40608093954460556376902031137928287845</ds:X509SerialNumber>
  //                      </xades:IssuerSerial>
  //                  </xades:Cert>
  //              </xades:SigningCertificate>
  //              <xades:SignaturePolicyIdentifier>
  //                  <xades:SignaturePolicyId>
  //                      <xades:SigPolicyId>
  //                          <xades:Identifier>https://tribunet.hacienda.go.cr/docs/esquemas/2017/v4.2/Resolucion%20Comprobantes%20Electronicos%20%20DGT-R-48-2016.pdf</xades:Identifier>
  //                          <xades:Description />
  //                      </xades:SigPolicyId>
  //                      <xades:SigPolicyHash>
  //                          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
  //                          <ds:DigestValue>NmI5Njk1ZThkNzI0MmIzMGJmZDAyNDc4YjUwNzkzODM2NTBiOWUxNTBkMmI2YjgzYzZjM2I5NTZlNDQ4OWQzMQ==</ds:DigestValue>
  //                      </xades:SigPolicyHash>
  //                  </xades:SignaturePolicyId>
  //              </xades:SignaturePolicyIdentifier>
  //          </xades:SignedSignatureProperties>
  //          <xades:SignedDataObjectProperties>
  //              <xades:DataObjectFormat ObjectReference="#Reference-ab26afbd-e2dc-4cb0-886a-ce2a4a118c7f">
  //                  <xades:MimeType>text/xml</xades:MimeType>
  //                  <xades:Encoding>UTF-8</xades:Encoding>
  //              </xades:DataObjectFormat>
  //          </xades:SignedDataObjectProperties>
  //      </xades:SignedProperties>
  //  </xades:QualifyingProperties>
  //  </ds:Object>
  //  </ds:Signature></FacturaElectronica>
  //  


  cert.Free;
  sbXml.Free;
  gen.Free;
  xml.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.