Zig
Zig
Create XAdES for Malaysia E-Invoice
See more Malaysia MyInvois Examples
This example signs XML to create the required XAdES for Malaysia E-Invoice as described at the following web pages:
https://sdk.myinvois.hasil.gov.my/signature/
https://sdk.myinvois.hasil.gov.my/signature-creation/
Note: This example requires Chilkat 10.0.0 or later.
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 XML such as the following to be signed:
// <?xml version="1.0" encoding="utf-8"?>
// <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
// <ext:UBLExtensions>
// <ext:UBLExtension>
// <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI>
// <ext:ExtensionContent>
// <sig:UBLDocumentSignatures xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">
// <sac:SignatureInformation>
// <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID>
// <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoice</sbc:ReferencedSignatureID>
// </sac:SignatureInformation>
// </sig:UBLDocumentSignatures>
// </ext:ExtensionContent>
// </ext:UBLExtension>
// </ext:UBLExtensions>
// <cbc:ID>IV00000105</cbc:ID>
// <cbc:IssueDate>2024-07-06</cbc:IssueDate>
// <cbc:IssueTime>15:06:00Z</cbc:IssueTime>
// <cbc:InvoiceTypeCode listVersionID="1.1">1</cbc:InvoiceTypeCode>
// <cbc:DocumentCurrencyCode>MYR</cbc:DocumentCurrencyCode>
// <cac:BillingReference>
// <cac:AdditionalDocumentReference>
// <cbc:ID>IV00000105</cbc:ID>
// </cac:AdditionalDocumentReference>
// </cac:BillingReference>
// <cac:Signature>
// <cbc:ID>urn:oasis:names:specification:ubl:signature:Invoice</cbc:ID>
// <cbc:SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped:xades</cbc:SignatureMethod>
// </cac:Signature>
// <cac:AccountingSupplierParty>
// <cac:Party>
// <cbc:IndustryClassificationCode name="Other information technology service activities n.e.c.">62099</cbc:IndustryClassificationCode>
// <cac:PartyIdentification>
// <cbc:ID schemeID="TIN">C99999999999</cbc:ID>
// </cac:PartyIdentification>
// <cac:PartyIdentification>
// <cbc:ID schemeID="BRN">200801099999</cbc:ID>
// </cac:PartyIdentification>
// <cac:PostalAddress>
// <cbc:CityName>Cheras</cbc:CityName>
// <cbc:PostalZone>56000</cbc:PostalZone>
// <cbc:CountrySubentityCode>14</cbc:CountrySubentityCode>
// <cac:AddressLine>
// <cbc:Line>A-3, 11, Jalan 2/199a,</cbc:Line>
// </cac:AddressLine>
// <cac:AddressLine>
// <cbc:Line>Cheras, 56000 Cheras</cbc:Line>
// </cac:AddressLine>
// <cac:AddressLine>
// <cbc:Line>Wilayah Persekutuan Kuala Lumpur</cbc:Line>
// </cac:AddressLine>
// <cac:Country>
// <cbc:IdentificationCode listAgencyID="6" listID="ISO3166-1">MYS</cbc:IdentificationCode>
// </cac:Country>
// </cac:PostalAddress>
// <cac:PartyLegalEntity>
// <cbc:RegistrationName>ABC SYSTEMS SERVICES</cbc:RegistrationName>
// </cac:PartyLegalEntity>
// <cac:Contact>
// <cbc:Telephone>019-626 9999</cbc:Telephone>
// <cbc:ElectronicMail>Info@abc.com.my</cbc:ElectronicMail>
// </cac:Contact>
// </cac:Party>
// </cac:AccountingSupplierParty>
// <cac:AccountingCustomerParty>
// <cac:Party>
// <cac:PartyIdentification>
// <cbc:ID schemeID="TIN">C99999999999</cbc:ID>
// </cac:PartyIdentification>
// <cac:PartyIdentification>
// <cbc:ID schemeID="BRN">200801099999</cbc:ID>
// </cac:PartyIdentification>
// <cac:PostalAddress>
// <cbc:CityName>Cheras</cbc:CityName>
// <cbc:PostalZone>56000</cbc:PostalZone>
// <cbc:CountrySubentityCode>14</cbc:CountrySubentityCode>
// <cac:AddressLine>
// <cbc:Line>A-3, 11, Jalan 2/199a,</cbc:Line>
// </cac:AddressLine>
// <cac:AddressLine>
// <cbc:Line>Cheras, 56000 Cheras</cbc:Line>
// </cac:AddressLine>
// <cac:AddressLine>
// <cbc:Line>Cheras, 56000 Cheras</cbc:Line>
// </cac:AddressLine>
// <cac:Country>
// <cbc:IdentificationCode listAgencyID="6" listID="ISO3166-1">MYS</cbc:IdentificationCode>
// </cac:Country>
// </cac:PostalAddress>
// <cac:PartyLegalEntity>
// <cbc:RegistrationName>ABC SYSTEMS SERVICES</cbc:RegistrationName>
// </cac:PartyLegalEntity>
// <cac:Contact>
// <cbc:Telephone>019-626 9999</cbc:Telephone>
// <cbc:ElectronicMail>Info@abc.com.my</cbc:ElectronicMail>
// </cac:Contact>
// </cac:Party>
// </cac:AccountingCustomerParty>
// <cac:TaxTotal>
// <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
// <cac:TaxSubtotal>
// <cbc:TaxableAmount currencyID="MYR">40</cbc:TaxableAmount>
// <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
// <cac:TaxCategory>
// <cbc:ID>6</cbc:ID>
// <cbc:Percent>0</cbc:Percent>
// <cac:TaxScheme>
// <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">OTH</cbc:ID>
// </cac:TaxScheme>
// </cac:TaxCategory>
// </cac:TaxSubtotal>
// </cac:TaxTotal>
// <cac:LegalMonetaryTotal>
// <cbc:LineExtensionAmount currencyID="MYR">40</cbc:LineExtensionAmount>
// <cbc:TaxExclusiveAmount currencyID="MYR">40</cbc:TaxExclusiveAmount>
// <cbc:TaxInclusiveAmount currencyID="MYR">49</cbc:TaxInclusiveAmount>
// <cbc:AllowanceTotalAmount currencyID="MYR">0</cbc:AllowanceTotalAmount>
// <cbc:PayableAmount currencyID="MYR">49</cbc:PayableAmount>
// </cac:LegalMonetaryTotal>
// <cac:InvoiceLine>
// <cbc:ID>1</cbc:ID>
// <cbc:InvoicedQuantity unitCode="H87">2</cbc:InvoicedQuantity>
// <cbc:LineExtensionAmount currencyID="MYR">40</cbc:LineExtensionAmount>
// <cac:TaxTotal>
// <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
// <cac:TaxSubtotal>
// <cbc:TaxableAmount currencyID="MYR">40</cbc:TaxableAmount>
// <cbc:TaxAmount currencyID="MYR">0</cbc:TaxAmount>
// <cac:TaxCategory>
// <cbc:ID>6</cbc:ID>
// <cbc:Percent>0</cbc:Percent>
// <cac:TaxScheme>
// <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">OTH</cbc:ID>
// </cac:TaxScheme>
// </cac:TaxCategory>
// </cac:TaxSubtotal>
// </cac:TaxTotal>
// <cac:Item>
// <cbc:Description>Computer Monitor 24 inch</cbc:Description>
// <cac:CommodityClassification>
// <cbc:ItemClassificationCode listID="CLASS">3</cbc:ItemClassificationCode>
// </cac:CommodityClassification>
// </cac:Item>
// <cac:Price>
// <cbc:PriceAmount currencyID="MYR">20</cbc:PriceAmount>
// </cac:Price>
// <cac:ItemPriceExtension>
// <cbc:Amount currencyID="MYR">40</cbc:Amount>
// </cac:ItemPriceExtension>
// </cac:InvoiceLine>
// </Invoice>
const xml_to_sign = try chilkat.Xml.init();
defer xml_to_sign.deinit();
xml_to_sign.loadXmlFile("qa_data/xml/myinvois_notYetSigned.xml") catch {
std.debug.print("Failed to load the XML to be signed.\n", .{});
return;
};
const gen = try chilkat.XmlDSigGen.init();
defer gen.deinit();
// ------------------------------------------------------------------------------------------------
// This behavior is required for the peculiarities of the MyInvois implementation.
// It requires Chilkat 10.0.0 or later.
gen.setBehaviors("MyInvois");
// ------------------------------------------------------------------------------------------------
gen.setSigLocation("Invoice|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation");
gen.setSigLocationMod(0);
gen.setSigId("signature");
gen.setSigNamespacePrefix("ds");
gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
gen.setSignedInfoCanonAlg("C14N_11");
gen.setSignedInfoDigestMethod("sha256");
// Create an Object to be added to the Signature.
const object1 = try chilkat.Xml.init();
defer object1.deinit();
object1.setTag("xades:QualifyingProperties");
object1.addAttribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#") catch {};
object1.addAttribute("Target", "signature") catch {};
object1.updateAttrAt("xades:SignedProperties", true, "Id", "id-xades-signed-props") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT");
object1.updateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256") catch {};
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT");
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName", "TO BE GENERATED BY CHILKAT");
object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber", "TO BE GENERATED BY CHILKAT");
gen.addObject("", try object1.getXml(alloc), "", "") catch {};
// -------- Reference 1 --------
const xml1 = try chilkat.Xml.init();
defer xml1.deinit();
xml1.setTag("ds:Transforms");
xml1.updateAttrAt("ds:Transform", true, "Algorithm", "http://www.w3.org/TR/1999/REC-xpath-19991116") catch {};
xml1.updateChildContent("ds:Transform|ds:XPath", "not(//ancestor-or-self::ext:UBLExtensions)");
xml1.updateAttrAt("ds:Transform[1]", true, "Algorithm", "http://www.w3.org/TR/1999/REC-xpath-19991116") catch {};
xml1.updateChildContent("ds:Transform[1]|ds:XPath", "not(//ancestor-or-self::cac:Signature)");
xml1.updateAttrAt("ds:Transform[2]", true, "Algorithm", "http://www.w3.org/2006/12/xml-c14n11") catch {};
gen.addSameDocRef2("", "sha256", xml1, "") catch {};
gen.setRefIdAttr("", "id-doc-signed-data") catch {};
// -------- Reference 2 --------
gen.addObjectRef("id-xades-signed-props", "sha256", "", "", "http://www.w3.org/2000/09/xmldsig#SignatureProperties") 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.setEmitCompact(true);
xml_to_sign.getXmlSb(sb_xml) catch {};
// 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;
};
// ----------------------------------------
// Make sure to indicate that we are verifying a MyInvois signature
// This is because MyInvois does things in a non-standard way..
verifier.setUncommonOptions("MyInvois");
// ----------------------------------------
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", .{});
}