Zig
Zig
TicketBAI Sign XML Example 2
See more TicketBAI Examples
Second example to demonstrate how to sign XML for TicketBAI.Chilkat Zig Downloads
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 XML to be signed...
const xml_to_sign = try chilkat.Xml.init();
defer xml_to_sign.deinit();
xml_to_sign.setTag("T:TicketBai");
xml_to_sign.addAttribute("xmlns:T", "urn:ticketbai:emision") catch {};
xml_to_sign.updateChildContent("Cabecera|IDVersionTBAI", "1.2");
xml_to_sign.updateChildContent("Sujetos|Emisor|NIF", "79732487C");
xml_to_sign.updateChildContent("Sujetos|Emisor|ApellidosNombreRazonSocial", "ARRIOLA LEJARDI ANE");
xml_to_sign.updateChildContent("Sujetos|Destinatarios|IDDestinatario|NIF", "A48012363");
xml_to_sign.updateChildContent("Sujetos|Destinatarios|IDDestinatario|ApellidosNombreRazonSocial", "ACADEMIA ESKOLA");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|SerieFactura", "A2022");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|NumFactura", "0400");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|FechaExpedicionFactura", "12-05-2022");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|HoraExpedicionFactura", "12:20:15");
xml_to_sign.updateChildContent("Factura|DatosFactura|DescripcionFactura", "Venta maquinaria y existencias");
xml_to_sign.updateChildContent("Factura|DatosFactura|ImporteTotalFactura", "1936.00");
xml_to_sign.updateChildContent("Factura|DatosFactura|Claves|IDClave|ClaveRegimenIvaOpTrascendencia", "01");
xml_to_sign.updateChildContent("Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|TipoNoExenta", "S1");
xml_to_sign.updateChildContent("Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|DesgloseIVA|DetalleIVA|BaseImponible", "1600.00");
xml_to_sign.updateChildContent("Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|DesgloseIVA|DetalleIVA|TipoImpositivo", "21.00");
xml_to_sign.updateChildContent("Factura|TipoDesglose|DesgloseFactura|Sujeta|NoExenta|DetalleNoExenta|DesgloseIVA|DetalleIVA|CuotaImpuesto", "336.00");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|SerieFacturaAnterior", "A2022");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|NumFacturaAnterior", "0399");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|FechaExpedicionFacturaAnterior", "06-05-2022");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|SignatureValueFirmaFacturaAnterior", "KdxKFNXeNJuBZNjgvKEa9lICamXs9MHuin6R+yuFHZw9aMewuBvdhaMr7g0eUz8aNYEa1elpCTr7aLgVZjQrFQK/TjIg353iAVxA");
xml_to_sign.updateChildContent("HuellaTBAI|Software|LicenciaTBAI", "TBAIPRUEBA");
xml_to_sign.updateChildContent("HuellaTBAI|Software|EntidadDesarrolladora|NIF", "A48119820");
xml_to_sign.updateChildContent("HuellaTBAI|Software|Nombre", "DFBTBAI");
xml_to_sign.updateChildContent("HuellaTBAI|Software|Version", "1.04.00");
xml_to_sign.updateChildContent("HuellaTBAI|NumSerieDispositivo", "GP4FC5J");
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
gen.setSigLocation("T:TicketBai");
gen.setSigLocationMod(0);
gen.setSigId("Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-Signature");
gen.setSigNamespacePrefix("ds");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSigValueId("Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-SignatureValue");
gen.setSignedInfoCanonAlg("C14N");
gen.setSignedInfoDigestMethod("sha256");
// Set the KeyInfoId before adding references..
gen.setKeyInfoId("Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-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("Id", "Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-QualifyingProperties") catch {};
object1.addAttribute("Target", "#Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-Signature") catch {};
object1.addAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") catch {};
object1.updateAttrAt("xades:SignedProperties", true, "Id", "Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-SignedProperties") 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#sha512") 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.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyId|xades:Identifier", "https://www.batuz.eus/fitxategiak/batuz/ticketbai/sinadura_elektronikoaren_zehaztapenak_especificaciones_de_la_firma_electronica_v1_0.pdf");
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyId|xades:Description", "");
object1.updateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyHash|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyHash|ds:DigestValue", "Quzn98x3PMbSHwbUzaj5f5KOpiH0u8bvmwbbbNkO9Es=");
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SignaturePolicyIdentifier|xades:SignaturePolicyId|xades:SigPolicyQualifiers|xades:SigPolicyQualifier|xades:SPURI", "https://www.batuz.eus/fitxategiak/batuz/ticketbai/sinadura_elektronikoaren_zehaztapenak_especificaciones_de_la_firma_electronica_v1_0.pdf");
object1.updateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat", true, "ObjectReference", "#Reference-43b5c8a3-7ce0-4056-bdbe-d2076d8ac825") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Description", "");
object1.updateAttrAt("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", true, "Qualifier", "OIDAsURN") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Identifier", "urn:oid:1.2.840.10003.5.109.10");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:ObjectIdentifier|xades:Description", "");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:MimeType", "text/xml");
object1.updateChildContent("xades:SignedProperties|xades:SignedDataObjectProperties|xades:DataObjectFormat|xades:Encoding", "");
gen.addObject("", try object1.getXml(alloc), "", "") catch {};
// -------- Reference 1 --------
gen.addSameDocRef("", "sha512", "C14N", "", "http://www.w3.org/2000/09/xmldsig#Object") catch {};
gen.setRefIdAttr("", "Reference-43b5c8a3-7ce0-4056-bdbe-d2076d8ac825") catch {};
// -------- Reference 2 --------
gen.addObjectRef("Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-SignedProperties", "sha512", "", "", "http://uri.etsi.org/01903#SignedProperties") catch {};
// -------- Reference 3 --------
gen.addSameDocRef("Signature-43861174-a0e6-45f9-afbf-8b48c46c6c13-KeyInfo", "sha512", "", "", "") 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,TransformSignatureXPath");
// 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_2.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", .{});
}