Zig
Zig
Paraguay E-Invoicing Signed XML (SIFEN)
See more XML Digital Signatures Examples
Demonstrates how to sign XML for e-invoicing -- SISTEMA INTEGRADO DE FACTURACIÓN ELECTRÓNICA NACIONAL (SIFEN). This is for signing XML files that need to be submitted to a government agency (Country Paraguay, SIFEN / EKUATIA, Sistema Nacional de Facturacion Electrónica).Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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("rDE");
xml_to_sign.addAttribute("xmlns", "http://ekuatia.set.gov.py/sifen/xsd") catch {};
xml_to_sign.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") catch {};
xml_to_sign.addAttribute("xsi:schemaLocation", "http://ekuatia.set.gov.py/sifen/xsd siRecepDE_v150.xsd") catch {};
xml_to_sign.updateChildContent("dVerFor", "150");
xml_to_sign.updateAttrAt("DE", true, "Id", "01800228774059001001501012022091519999999990") catch {};
xml_to_sign.updateChildContent("DE|dDVId", "0");
xml_to_sign.updateChildContent("DE|dFecFirma", "2022-09-15T10:23:05");
xml_to_sign.updateChildContent("DE|dSisFact", "1");
xml_to_sign.updateChildContent("DE|gOpeDE|iTipEmi", "1");
xml_to_sign.updateChildContent("DE|gOpeDE|dDesTipEmi", "Normal");
xml_to_sign.updateChildContent("DE|gOpeDE|dCodSeg", "999999999");
xml_to_sign.updateChildContent("DE|gOpeDE|dInfoEmi", "GENERADA DE PEDIDO 131043202");
xml_to_sign.updateChildContent("DE|gTimb|iTiDE", "1");
xml_to_sign.updateChildContent("DE|gTimb|dDesTiDE", "Factura electr�nica");
xml_to_sign.updateChildContent("DE|gTimb|dNumTim", "18889014");
xml_to_sign.updateChildContent("DE|gTimb|dEst", "059");
xml_to_sign.updateChildContent("DE|gTimb|dPunExp", "001");
xml_to_sign.updateChildContent("DE|gTimb|dNumDoc", "0015010");
xml_to_sign.updateChildContent("DE|gTimb|dFeIniT", "2022-05-25");
xml_to_sign.updateChildContent("DE|gDatGralOpe|dFeEmiDE", "2022-09-15T10:22:57");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gOpeCom|iTipTra", "1");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gOpeCom|dDesTipTra", "Venta de mercader�a");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gOpeCom|iTImp", "1");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gOpeCom|dDesTImp", "IVA");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gOpeCom|cMoneOpe", "PYG");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gOpeCom|dDesMoneOpe", "Guarani");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dRucEm", "80022877");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dDVEmi", "4");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|iTipCont", "1");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|cTipReg", "1");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dNomEmi", "ABCDEF SA");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dNomFanEmi", "ABCDEF SA");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dDirEmi", "Jos� Abcde Xyz N� 666 e/Abxyz123");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dNumCas", "0");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dCompDir1", "N/A");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dCompDir2", "N/A");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|cDepEmi", "12");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dDesDepEmi", "CENTRAL");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|cDisEmi", "153");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dDesDisEmi", "CAPIATA");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|cCiuEmi", "1234");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dDesCiuEmi", "CAPIATA");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dTelEmi", "(022) 1111 000");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dEmailE", "somebody@example.com.py");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|dDenSuc", "PUNTO 99");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|gActEco|cActEco", "47721");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gEmis|gActEco|dDesActEco", "COMERCIO AL POR MENOR DE PRODUCTOS");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|iNatRec", "1");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|iTiOpe", "2");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|cPaisRec", "PRY");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dDesPaisRe", "Paraguay");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|iTiContRec", "2");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dRucRec", "80082971");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dDVRec", "9");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dNomRec", "XXXXXXX");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dNomFanRec", "XXXXXXX");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dNumCasRec", "0");
xml_to_sign.updateChildContent("DE|gDatGralOpe|gDatRec|dCodCliente", "9999999");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamFE|iIndPres", "1");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamFE|dDesIndPres", "Operaci�n presencial");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|iCondOpe", "1");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|dDCondOpe", "Contado");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni|iTiPago", "1");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni|dDesTiPag", "Efectivo");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni|dMonTiPag", "100000.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni|cMoneTiPag", "PYG");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni|dDMoneTiPag", "Guarani");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni[1]|iTiPago", "1");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni[1]|dDesTiPag", "Efectivo");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni[1]|dMonTiPag", "73600.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni[1]|cMoneTiPag", "PYG");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamCond|gPaConEIni[1]|dDMoneTiPag", "Guarani");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|dCodInt", "818181");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|dDesProSer", "Widget Abc Xyz");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|cUniMed", "77");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|dDesUniMed", "UNI");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|dCantProSer", "2.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gValorItem|dPUniProSer", "16500.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gValorItem|dTotBruOpeItem", "33000.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dDescItem", "3300.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dPorcDesIt", "20.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gValorItem|gValorRestaItem|dTotOpeItem", "26400.0000");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gCamIVA|iAfecIVA", "1");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gCamIVA|dDesAfecIVA", "Gravado IVA");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gCamIVA|dPropIVA", "100");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gCamIVA|dTasaIVA", "5");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gCamIVA|dBasGravIVA", "25142.8571");
xml_to_sign.updateChildContent("DE|gDtipDE|gCamItem|gCamIVA|dLiqIVAItem", "1257.1429");
xml_to_sign.updateChildContent("DE|gTotSub|dSubExe", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dSub5", "26400.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dSub10", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotOpe", "26400.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotDesc", "6600.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotDescGlotem", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotAntItem", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotAnt", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dPorcDescTotal", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dDescTotal", "6600.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dAnticipo", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dRedon", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotGralOpe", "26400.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dIVA5", "1257.1429");
xml_to_sign.updateChildContent("DE|gTotSub|dIVA10", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTotIVA", "1257.1429");
xml_to_sign.updateChildContent("DE|gTotSub|dBaseGrav5", "25142.8571");
xml_to_sign.updateChildContent("DE|gTotSub|dBaseGrav10", "0.0000");
xml_to_sign.updateChildContent("DE|gTotSub|dTBasGraIVA", "25142.8571");
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
gen.setSigLocation("rDE");
gen.setSigLocationMod(0);
gen.setSigNamespacePrefix("");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSignedInfoCanonAlg("C14N");
gen.setSignedInfoDigestMethod("sha256");
// -------- Reference 1 --------
const xml1 = try chilkat.Xml.init();
defer xml1.deinit();
xml1.setTag("Transforms");
xml1.updateAttrAt("Transform", true, "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature") catch {};
xml1.updateAttrAt("Transform[1]", true, "Algorithm", "http://www.w3.org/2001/10/xml-exc-c14n#") catch {};
gen.addSameDocRef2("01800228774059001001501012022091516941989060", "sha256", xml1, "") 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("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");
// 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("c:/temp/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", .{});
}