Sample code for 30+ languages & platforms
Unicode C

Alliance Access LAU Sign Message (XML Signature using HMAC-SHA-256)

See more XML Digital Signatures Examples

Demonstrates how to sign XML according to the requirements for Alliance Access LAU (Local Authentication) using HMAC-SHA-256.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xmlToSign;
    HCkXmlDSigGenW gen;
    HCkStringBuilderW sbXml;

    success = FALSE;

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

    //  We begin with this message:

    //  <?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:SwGbl="urn:swift:snl:ns.SwGbl" xmlns:SwInt="urn:swift:snl:ns:SwInt" xmlns:SwSec="url:swift:snl:ns.SwSec">
    //      <Saa:Revision>2.0.7</Saa:Revision>
    //      <Saa:Header>
    //          <Saa:Message>
    //  			<test>blah blah</test>
    //          </Saa:Message>
    //      </Saa:Header>
    //      <Saa:Body>...</Saa:Body>
    //      <Saa:LAU>
    //      </Saa:LAU>
    //  </Saa:DataPDU>

    //  And we want so sign to create this as the result:
    //  The signed XML we'll create will not be indented and pretty-printed like this.
    //  Instead, we'll use the "CompactSignedXml" behavior to produce compact single-line 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:SwGbl="urn:swift:snl:ns.SwGbl" xmlns:SwInt="urn:swift:snl:ns:SwInt" xmlns:SwSec="url:swift:snl:ns.SwSec">
    //      <Saa:Revision>2.0.7</Saa:Revision>
    //      <Saa:Header>
    //          <Saa:Message>
    //  			<test>blah blah</test>
    //          </Saa:Message>
    //      </Saa:Header>
    //      <Saa:Body>...</Saa:Body>
    //      <Saa:LAU>
    //          <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    //              <ds:SignedInfo>
    //                  <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                  <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#hmac-sha256"/>
    //                  <ds:Reference URI="">
    //                      <ds:Transforms>
    //                          <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
    //                          <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                      </ds:Transforms>
    //                      <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                      <ds:DigestValue>Y7oScHnYOUQvni/TSzZbDec+HR+mWIFH149GXpwj1Ws=</ds:DigestValue>
    //                  </ds:Reference>
    //              </ds:SignedInfo>
    //              <ds:SignatureValue>6ynF/FcwbPsHrtlj3h2agJigdnvpbO6hOzKSRGzqkw0=</ds:SignatureValue>
    //          </ds:Signature>
    //      </Saa:LAU>
    //  </Saa:DataPDU>

    success = TRUE;

    //  Create the XML to be signed...

    //  (The XML does not need to be created this way.  It can be loaded from a file or a string.)
    //  Also, use this online tool to generate code from sample XML: 
    //  Generate Code to Create XML

    xmlToSign = CkXmlW_Create();
    CkXmlW_putTag(xmlToSign,L"Saa:DataPDU");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:Saa",L"urn:swift:saa:xsd:saa.2.0");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:Sw",L"urn:swift:snl:ns.Sw");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:SwGbl",L"urn:swift:snl:ns.SwGbl");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:SwInt",L"urn:swift:snl:ns:SwInt");
    CkXmlW_AddAttribute(xmlToSign,L"xmlns:SwSec",L"url:swift:snl:ns.SwSec");
    CkXmlW_UpdateChildContent(xmlToSign,L"Saa:Revision",L"2.0.7");
    CkXmlW_UpdateChildContent(xmlToSign,L"Saa:Header|Saa:Message|test",L"blah blah");
    CkXmlW_UpdateChildContent(xmlToSign,L"Saa:Body",L"...");
    CkXmlW_UpdateChildContent(xmlToSign,L"Saa:LAU",L"");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"Saa:DataPDU|Saa: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");

    //  You may alternatively choose "IndentedSignature" instead of "CompactSignedXml"
    CkXmlDSigGenW_putBehaviors(gen,L"CompactSignedXml");

    CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha256",L"EXCL_C14N",L"",L"");

    //  Specify the HMAC key.
    //  For example, if the HMAC key is to be the us-ascii bytes of the string "secret",
    //  the HMAC key can be set in any of the following ways (and also more ways not shown here..)
    CkXmlDSigGenW_SetHmacKey(gen,L"secret",L"ascii");
    //  or
    CkXmlDSigGenW_SetHmacKey(gen,L"c2VjcmV0",L"base64");
    //  or
    CkXmlDSigGenW_SetHmacKey(gen,L"736563726574",L"hex");

    //  Sign the XML..
    sbXml = CkStringBuilderW_Create();
    CkXmlW_GetXmlSb(xmlToSign,sbXml);
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

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

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


    CkXmlW_Dispose(xmlToSign);
    CkXmlDSigGenW_Dispose(gen);
    CkStringBuilderW_Dispose(sbXml);

    }