Sample code for 30+ languages & platforms
Lua

Calculate HRMC IRMark

See more XML Digital Signatures Examples

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

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- 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.
local xml = chilkat.Xml.new()
xml.tag = "GovTalkMessage"
xml:add_attribute("xmlns", "http://www.govtalk.gov.uk/CM/envelope")
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>

print(xml:get_xml())

-- Canonicalize the XML (using C14N)
local xdsig = chilkat.XmlDSig.new()
local withComments = false
local canonXml = xdsig:canonicalize_xml(xml:get_xml(), "C14N", withComments)

-- Return the base64 SHA-1 HASH of the canonical XML.
local crypt = chilkat.Crypt2.new()
crypt.hash_algorithm = "sha1"
crypt.encoding_mode = "base64"
local irmark = crypt:hash_string_enc(canonXml)

print("IRMark = " .. irmark)