Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Alliance Access LAU Sign Message (XML Signature using HMAC-SHA-256)Demonstrates how to sign XML according to the requirements for Alliance Access LAU (Local Authentication) using HMAC-SHA-256.
integer li_rc integer li_Success oleobject loo_XmlToSign oleobject loo_Gen oleobject loo_SbXml // 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> li_Success = 1 // 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 loo_XmlToSign = create oleobject // Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 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 loo_XmlToSign.Tag = "Saa:DataPDU" loo_XmlToSign.AddAttribute("xmlns:Saa","urn:swift:saa:xsd:saa.2.0") loo_XmlToSign.AddAttribute("xmlns:Sw","urn:swift:snl:ns.Sw") loo_XmlToSign.AddAttribute("xmlns:SwGbl","urn:swift:snl:ns.SwGbl") loo_XmlToSign.AddAttribute("xmlns:SwInt","urn:swift:snl:ns:SwInt") loo_XmlToSign.AddAttribute("xmlns:SwSec","url:swift:snl:ns.SwSec") loo_XmlToSign.UpdateChildContent("Saa:Revision","2.0.7") loo_XmlToSign.UpdateChildContent("Saa:Header|Saa:Message|test","blah blah") loo_XmlToSign.UpdateChildContent("Saa:Body","...") loo_XmlToSign.UpdateChildContent("Saa:LAU","") loo_Gen = create oleobject // Use "Chilkat_9_5_0.XmlDSigGen" for versions of Chilkat < 10.0.0 li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen") loo_Gen.SigLocation = "Saa:DataPDU|Saa:LAU" loo_Gen.SigLocationMod = 0 loo_Gen.SigNamespacePrefix = "ds" loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" loo_Gen.SignedInfoCanonAlg = "EXCL_C14N" loo_Gen.SignedInfoDigestMethod = "sha256" // You may alternatively choose "IndentedSignature" instead of "CompactSignedXml" loo_Gen.Behaviors = "CompactSignedXml" loo_Gen.AddSameDocRef("","sha256","EXCL_C14N","","") // 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..) loo_Gen.SetHmacKey("secret","ascii") // or loo_Gen.SetHmacKey("c2VjcmV0","base64") // or loo_Gen.SetHmacKey("736563726574","hex") // Sign the XML.. loo_SbXml = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder") loo_XmlToSign.GetXmlSb(loo_SbXml) 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_SbXml return end if // Save the signed XML to a file. li_Success = loo_SbXml.WriteFile("qa_output/signedXml.xml","utf-8",0) // Show the signed XML. Write-Debug loo_SbXml.GetAsString() destroy loo_XmlToSign destroy loo_Gen destroy loo_SbXml |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.