Sample code for 30+ languages & platforms
Zig

Calculate HRMC IRMark

See more XML Digital Signatures Examples

Demonstrates how to calculate an IRmark for an HMRC GovTalkMessage XML document.

Chilkat Zig Downloads

Zig
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.

    // First build a sample (but incomplete) GovTalkMessage.
    // You can replace this code with a call to LoadXml or LoadXmlFile..
    //
    // Note: The online tool at https://tools.chilkat.io/xmlCreate.cshtml
    // can be used to generate XML code that creates a given XML document.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.setTag("GovTalkMessage");
    xml.addAttribute("xmlns", "http://www.govtalk.gov.uk/CM/envelope") catch {};
    xml.updateAttrAt("Body|Message", true, "xmlns", "urn:g3.ge:dea:call:SendDocument:v1") catch {};
    xml.updateChildContent("Body|Message", "................");

    // This is the XML created:
    // <?xml version="1.0" encoding="utf-8" ?>
    // <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
    //     <Body>
    //         <Message xmlns="urn:g3.ge:dea:call:SendDocument:v1">................</Message>
    //     </Body>
    // </GovTalkMessage>

    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Canonicalize the XML (using C14N)
    const xdsig = try chilkat.XmlDSig.init();
    defer xdsig.deinit();
    const with_comments = false;
    const canon_xml = try xdsig.canonicalizeXml(alloc, try xml.getXml(alloc), "C14N", with_comments);

    // Return the base64 SHA-1 HASH of the canonical XML.
    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();
    crypt.setHashAlgorithm("sha1");
    crypt.setEncodingMode("base64");
    const irmark = try crypt.hashStringENC(alloc, canon_xml);

    std.debug.print("IRMark = {s}\n", .{irmark});
}