Sample code for 30+ languages & platforms
Rust

Calculate HRMC IRMark

See more XML Digital Signatures Examples

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

Chilkat Rust Downloads

Rust
// 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.
let xml = chilkat::Xml::new();
xml.set_tag("GovTalkMessage");
let _ = xml.add_attribute("xmlns", "http://www.govtalk.gov.uk/CM/envelope");
let _ = xml.update_attr_at("Body|Message", true, "xmlns", "urn:g3.ge:dea:call:SendDocument:v1");
xml.update_child_content("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>

println!("{}", xml.get_xml().unwrap_or_default());

// Canonicalize the XML (using C14N)
let xdsig = chilkat::XmlDSig::new();
let with_comments = false;
let canon_xml = xdsig.canonicalize_xml(&xml.get_xml().unwrap_or_default(), "C14N", with_comments).unwrap_or_default();

// Return the base64 SHA-1 HASH of the canonical XML.
let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("sha1");
crypt.set_encoding_mode("base64");
let irmark = crypt.hash_string_enc(&canon_xml).unwrap_or_default();

println!("IRMark = {}", irmark);