Sample code for 30+ languages & platforms
Unicode C

Swift Alliance LAU XML Signature

See more XML Digital Signatures Examples

Calculating the LAU for Swift Alliance File Act XML using HMAC SHA256. (The XML signature, also referred to as Local Authentication (LAU), is computed using the algorithm HMAC based on SHA-256.)

Chilkat Unicode C Downloads

Unicode C
#include <C_CkStringBuilderW.h>
#include <C_CkXmlDSigGenW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbXml;
    int numReplaced;
    HCkXmlDSigGenW gen;

    success = FALSE;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // We begin with the following XML:

    // <?xml version="1.0" encoding="UTF-8"?>
    // <Saa:DataPDU xmlns:Saa="urn:swift:saa:xsd:saa.2.0" xmlns:Sw="urn:swift:snl:ns.Sw" xmlns:SwInt="urn:swift:snl:ns.SwInt" xmlns:SwGbl="urn:swift:snl:ns.SwGbl" xmlns:SwSec="urn:swift:snl:ns.SwSec">
    //     <Saa:Revision>2.0.6</Saa:Revision>
    //     <Saa:Header>
    //         <Saa:Message>
    //             <Saa:SenderReference>Ref760FA1234</Saa:SenderReference>
    //             <Saa:MessageIdentifier>tsrv.fin.mt7xx.gteesstandbys</Saa:MessageIdentifier>
    //             <Saa:Format>File</Saa:Format>
    //             <Saa:SubFormat>Input</Saa:SubFormat>
    //             <Saa:Sender>
    //                 <Saa:DN>cn=su8,o=ptsqgbbb,o=swift</Saa:DN>
    //                 <Saa:FullName>
    //                     <Saa:X1>PTSQGBBBXXX</Saa:X1>
    //                 </Saa:FullName>
    //             </Saa:Sender>
    //             <Saa:Receiver>
    //                 <Saa:DN>cn=abc,ou=saa,o=xxx,o=swift</Saa:DN>
    //                 <Saa:FullName>
    //                     <Saa:X1>PTSXXXXXXXX</Saa:X1>
    //                     <Saa:X2>saa</Saa:X2>
    //                 </Saa:FullName>
    //             </Saa:Receiver>
    //             <Saa:InterfaceInfo>
    //                 <Saa:UserReference>CRE...</Saa:UserReference>
    //                 <Saa:MessageCreator>ApplicationInterface</Saa:MessageCreator>
    //                 <Saa:MessageContext>Original</Saa:MessageContext>
    //                 <Saa:MessageNature>Financial</Saa:MessageNature>
    //             </Saa:InterfaceInfo>
    //             <Saa:NetworkInfo>
    //                 <Saa:Priority>Normal</Saa:Priority>
    //                 <Saa:IsPossibleDuplicate>true</Saa:IsPossibleDuplicate>
    //                 <Saa:Service>swift.corp.fast!x</Saa:Service>
    //                 <Saa:Network>Application</Saa:Network>
    //                 <Saa:SessionNr>0080</Saa:SessionNr>
    //                 <Saa:SeqNr>000001</Saa:SeqNr>
    //                 <Saa:SWIFTNetNetworkInfo>
    //                     <Saa:RequestType>tsrv.fin.mt7xx.gteesstandbys</Saa:RequestType>
    //                     <Saa:Reference>2f949999-d32e-49eb-9999-9a819b9b9c0d</Saa:Reference>
    //                     <Saa:FileInfo>SwCompression=Zip</Saa:FileInfo>
    //                 </Saa:SWIFTNetNetworkInfo>
    //             </Saa:NetworkInfo>
    //             <Saa:SecurityInfo>
    //                 <Saa:SWIFTNetSecurityInfo>
    //                     <Saa:FileDigestAlgorithm>SHA-256</Saa:FileDigestAlgorithm>
    //                     <Saa:FileDigestValue>9tnnjIgsowPSU+ehm8Rb0J5TvZIvhCYnySzFkpur1aw=</Saa:FileDigestValue>
    //                 </Saa:SWIFTNetSecurityInfo>
    //             </Saa:SecurityInfo>
    //             <Saa:FileLogicalName>Payload.ZIP</Saa:FileLogicalName>
    //             <Saa:ExpiryDateTime>20210712074808</Saa:ExpiryDateTime>
    //         </Saa:Message>
    //     </Saa:Header>
    //     <Saa:Body>Payload.ZIP</Saa:Body>
    // </Saa:DataPDU>

    // Load the XML file to be signed.
    sbXml = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbXml,L"c:/someDir/fileToSign.xml",L"utf-8");
    if (success == FALSE) {
        wprintf(L"Failed to load XML file.\n");
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

    // We need to add a "LAU" XML element just before the "</Saa:DataPDU>"
    numReplaced = CkStringBuilderW_Replace(sbXml,L"</Saa:DataPDU>",L"<LAU xmlns=\"urn:swift:saa:xsd:saa.2.0\"></LAU></Saa:DataPDU>");

    gen = CkXmlDSigGenW_Create();
    CkXmlDSigGenW_putSigLocation(gen,L"Saa:DataPDU|LAU");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha256");
    CkXmlDSigGenW_putBehaviors(gen,L"CompactSignedXml");

    // Sign the entire document.
    CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha256",L"EXCL_C14N",L"",L"");

    // Set our HMAC key
    CkXmlDSigGenW_SetHmacKey(gen,L"Abcdef0123456789Abcdef0123456789",L"ascii");

    // Sign the XML:
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success == FALSE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkStringBuilderW_Dispose(sbXml);
        CkXmlDSigGenW_Dispose(gen);
        return;
    }

    // Examine the signed XML
    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbXml));

    // Save the signed XML to a file.
    success = CkStringBuilderW_WriteFile(sbXml,L"c:/someDir/signedFile.xml",L"utf-8",FALSE);


    CkStringBuilderW_Dispose(sbXml);
    CkXmlDSigGenW_Dispose(gen);

    }