Zig
Zig
SII XML Digital Signature
See more uncategorized Examples
Example for SII XML Digital Signature.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Load the XML to be signed.
const xml_to_sign = try chilkat.Xml.init();
defer xml_to_sign.deinit();
xml_to_sign.loadXmlFile("c:/aaworkarea/eduardo/sii_unsigned.xml") catch {
std.debug.print("{s}\n", .{try xml_to_sign.getLastErrorText(alloc)});
return;
};
// The sample XML to be signed looks like this:
// <?xml version="1.0" encoding="ISO-8859-1"?>
// <EnvioDTE xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd" version="1.0">
// <SetDTE ID="SetDocF0T33_20240425_170512">
// <Caratula version="1.0">
// <RutEmisor>99999999-4</RutEmisor>
// <RutEnvia>12345678-6</RutEnvia>
// <RutReceptor>888888000-K</RutReceptor>
// <FchResol>2014-08-22</FchResol>
// <NroResol>80</NroResol>
// <TmstFirmaEnv>2024-04-25T17:05:13</TmstFirmaEnv>
// <SubTotDTE>
// <TpoDTE>33</TpoDTE>
// <NroDTE>1</NroDTE>
// </SubTotDTE>
// </Caratula>
// <DTE version="1.0">
// <Documento ID="F555T55">
// ...
// </Documento>
// </EnvioDTE>
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
gen.setSigLocation("EnvioDTE|SetDTE|DTE");
gen.setSigLocationMod(0);
gen.setSigNamespacePrefix("");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSignedInfoCanonAlg("C14N");
gen.setSignedInfoDigestMethod("sha1");
// -------- Reference 1 --------
const xml1 = try chilkat.Xml.init();
defer xml1.deinit();
xml1.setTag("Transforms");
xml1.updateAttrAt("Transform", true, "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315") catch {};
gen.addSameDocRef2("F511T33", "sha1", 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+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");
// 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", .{});
}