Zig
Zig
TicketBAI Sign XML Example 1
See more TicketBAI Examples
First 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", "B00000034");
xml_to_sign.updateChildContent("Sujetos|Destinatarios|IDDestinatario|ApellidosNombreRazonSocial", "CONSULTORIA GALDETU");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|SerieFactura", "A2022");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|NumFactura", "0399");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|FechaExpedicionFactura", "06-05-2022");
xml_to_sign.updateChildContent("Factura|CabeceraFactura|HoraExpedicionFactura", "10:30:28");
xml_to_sign.updateChildContent("Factura|DatosFactura|DescripcionFactura", "Venta Fotocopister�a");
xml_to_sign.updateChildContent("Factura|DatosFactura|ImporteTotalFactura", "31.46");
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", "26.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", "5.46");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|SerieFacturaAnterior", "A2022");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|NumFacturaAnterior", "0398");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|FechaExpedicionFacturaAnterior", "05-05-2022");
xml_to_sign.updateChildContent("HuellaTBAI|EncadenamientoFacturaAnterior|SignatureValueFirmaFacturaAnterior", "KwXaQec65SKpVP7EU9o4nUXOx7SAftIToFsxH+2j2tXPXhpBUnS26dhdSpiMl2DlTuqRsFdZfWyYazaGHgSRQHZZAnFtpbi4BeMk");
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-63c35f38-2b5f-4600-b3da-3ddee86d62b3-Signature");
gen.setSigNamespacePrefix("ds");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSigValueId("Signature-63c35f38-2b5f-4600-b3da-3ddee86d62b3-SignatureValue");
gen.setSignedInfoCanonAlg("C14N");
gen.setSignedInfoDigestMethod("sha256");
// Set the KeyInfoId before adding references..
gen.setKeyInfoId("Signature-63c35f38-2b5f-4600-b3da-3ddee86d62b3-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-63c35f38-2b5f-4600-b3da-3ddee86d62b3-QualifyingProperties") catch {};
object1.addAttribute("Target", "#Signature-63c35f38-2b5f-4600-b3da-3ddee86d62b3-Signature") catch {};
object1.addAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#") catch {};
object1.updateAttrAt("xades:SignedProperties", true, "Id", "Signature-63c35f38-2b5f-4600-b3da-3ddee86d62b3-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-7e6f3481-4acc-47de-90fd-67878ad15e8e") 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-7e6f3481-4acc-47de-90fd-67878ad15e8e") catch {};
// -------- Reference 2 --------
gen.addObjectRef("Signature-63c35f38-2b5f-4600-b3da-3ddee86d62b3-SignedProperties", "sha512", "", "", "http://uri.etsi.org/01903#SignedProperties") catch {};
// -------- Reference 3 --------
gen.addSameDocRef("Signature-63c35f38-2b5f-4600-b3da-3ddee86d62b3-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("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_1.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", .{});
}