Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_XmlToSign
oleobject loo_Gen
oleobject loo_Object1
oleobject loo_Xml1
oleobject loo_Cert
oleobject loo_SbXml
oleobject loo_Verifier
integer li_NumSigs
integer li_VerifyIdx
integer li_Verified

li_Success = 0

li_Success = 1

// 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>

loo_XmlToSign = create oleobject
li_rc = loo_XmlToSign.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_XmlToSign
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_XmlToSign.LoadXmlFile("qa_data/xml/myinvois_notYetSigned.xml")
if li_Success = 0 then
    Write-Debug "Failed to load the XML to be signed."
    destroy loo_XmlToSign
    return
end if

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

// ------------------------------------------------------------------------------------------------
// This behavior is required for the peculiarities of the MyInvois implementation.
// It requires Chilkat 10.0.0 or later.
loo_Gen.Behaviors = "MyInvois"
// ------------------------------------------------------------------------------------------------

loo_Gen.SigLocation = "Invoice|ext:UBLExtensions|ext:UBLExtension|ext:ExtensionContent|sig:UBLDocumentSignatures|sac:SignatureInformation"
loo_Gen.SigLocationMod = 0
loo_Gen.SigId = "signature"
loo_Gen.SigNamespacePrefix = "ds"
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SignedInfoCanonAlg = "C14N_11"
loo_Gen.SignedInfoDigestMethod = "sha256"

// Create an Object to be added to the Signature.
loo_Object1 = create oleobject
li_rc = loo_Object1.ConnectToNewObject("Chilkat.Xml")

loo_Object1.Tag = "xades:QualifyingProperties"
loo_Object1.AddAttribute("xmlns:xades","http://uri.etsi.org/01903/v1.3.2#")
loo_Object1.AddAttribute("Target","signature")
loo_Object1.UpdateAttrAt("xades:SignedProperties",1,"Id","id-xades-signed-props")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime","TO BE GENERATED BY CHILKAT")
loo_Object1.UpdateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestMethod",1,"Algorithm","http://www.w3.org/2001/04/xmlenc#sha256")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:CertDigest|ds:DigestValue","TO BE GENERATED BY CHILKAT")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509IssuerName","TO BE GENERATED BY CHILKAT")
loo_Object1.UpdateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificate|xades:Cert|xades:IssuerSerial|ds:X509SerialNumber","TO BE GENERATED BY CHILKAT")

loo_Gen.AddObject("",loo_Object1.GetXml(),"","")

// -------- Reference 1 --------
loo_Xml1 = create oleobject
li_rc = loo_Xml1.ConnectToNewObject("Chilkat.Xml")

loo_Xml1.Tag = "ds:Transforms"
loo_Xml1.UpdateAttrAt("ds:Transform",1,"Algorithm","http://www.w3.org/TR/1999/REC-xpath-19991116")
loo_Xml1.UpdateChildContent("ds:Transform|ds:XPath","not(//ancestor-or-self::ext:UBLExtensions)")
loo_Xml1.UpdateAttrAt("ds:Transform[1]",1,"Algorithm","http://www.w3.org/TR/1999/REC-xpath-19991116")
loo_Xml1.UpdateChildContent("ds:Transform[1]|ds:XPath","not(//ancestor-or-self::cac:Signature)")
loo_Xml1.UpdateAttrAt("ds:Transform[2]",1,"Algorithm","http://www.w3.org/2006/12/xml-c14n11")

loo_Gen.AddSameDocRef2("","sha256",loo_Xml1,"")
loo_Gen.SetRefIdAttr("","id-doc-signed-data")

// -------- Reference 2 --------
loo_Gen.AddObjectRef("id-xades-signed-props","sha256","","","http://www.w3.org/2000/09/xmldsig#SignatureProperties")

// Provide a certificate + private key. (PFX password is test123)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Xml1
    destroy loo_Cert
    return
end if

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "X509Data"
loo_Gen.X509Type = "Certificate"

// Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_XmlToSign.EmitCompact = 1
loo_XmlToSign.GetXmlSb(loo_SbXml)

// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Xml1
    destroy loo_Cert
    destroy loo_SbXml
    return
end if

// -----------------------------------------------

// Save the signed XML to a file.
li_Success = loo_SbXml.WriteFile("c:/temp/qa_output/signedXml.xml","utf-8",0)

Write-Debug loo_SbXml.GetAsString()

// ----------------------------------------
// Verify the signatures we just produced...
loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.XmlDSig")

li_Success = loo_Verifier.LoadSignatureSb(loo_SbXml)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_XmlToSign
    destroy loo_Gen
    destroy loo_Object1
    destroy loo_Xml1
    destroy loo_Cert
    destroy loo_SbXml
    destroy loo_Verifier
    return
end if

// ----------------------------------------
// Make sure to indicate that we are verifying a MyInvois signature
// This is because MyInvois does things in a non-standard way..
loo_Verifier.UncommonOptions = "MyInvois"
// ----------------------------------------

li_NumSigs = loo_Verifier.NumSignatures
li_VerifyIdx = 0
do while li_VerifyIdx < li_NumSigs
    loo_Verifier.Selector = li_VerifyIdx
    li_Verified = loo_Verifier.VerifySignature(1)
    if li_Verified <> 1 then
        Write-Debug loo_Verifier.LastErrorText
        destroy loo_XmlToSign
        destroy loo_Gen
        destroy loo_Object1
        destroy loo_Xml1
        destroy loo_Cert
        destroy loo_SbXml
        destroy loo_Verifier
        return
    end if

    li_VerifyIdx = li_VerifyIdx + 1
loop
Write-Debug "All signatures were successfully verified."


destroy loo_XmlToSign
destroy loo_Gen
destroy loo_Object1
destroy loo_Xml1
destroy loo_Cert
destroy loo_SbXml
destroy loo_Verifier