Visual Basic Source: KeyFromHMACSecret.frm
Dim xmldoc As New DOMDocument50 Dim xmldsig As New MXDigitalSignature50 Dim dsigKey As IXMLDSigKey Dim DSIGNS, sigOut, sigTmp As String Const KEYVALUE = 1 Private Function LoadXML(ByVal file As String) ' Read the input xml file and display the content in the text3. Path = App.Path + "\" + file xmldoc.async = False xmldoc.preserveWhiteSpace = True xmldoc.validateOnParse = False If xmldoc.Load(Path) = False Then Text1.Text = Text1.Text + "can't load " + Path + vbNewLine _ + xmldoc.parseError.reason + vbNewLine LoadXML = False Exit Function End If xmldoc.setProperty "SelectionNamespaces", DSIGNS Set xmldsig.signature = xmldoc.selectSingleNode(".//ds:Signature") LoadXML = True End Function Private Function SignXML(ByVal secret As String, ByVal length As Long) If xmldsig.signature Is Nothing Then Text1.Text = Text1.Text + "Invalid signature template." SignXML = False Exit Function End If Set oKey = xmldsig.createKeyFromHMACSecret(secret, length) If oKey Is Nothing Then Text1.Text = Text1.Text + _ "Failed to create key from HMAC secret value\n" SignXML = False Exit Function End If Set oSignedKey = xmldsig.sign(oKey, KEYVALUE) If oSignedKey Is Nothing Then Text1.Text = Text1.Text + "sign failed." + vbNewLine SignXML = False Exit Function End If xmldoc.save sigOut Text1.Text = Text1.Text _ + "The data referenced in the signature template " _ + "was signed successfully." + vbNewLine _ + "Resultant signature:" + vbNewLine + vbNewLine _ + xmldoc.xml + vbNewLine SignXML = True End Function Private Function VerifyXML(ByVal secret As String, ByVal length As Long) If xmldsig.signature Is Nothing Then Text1.Text = Text1.Text + "Invalid signature object." VerifyXML = False Exit Function End If Set oKey = xmldsig.createKeyFromHMACSecret(secret, length) If oKey Is Nothing Then Text1.Text = Text1.Text + _ "Failed to create key from HMAC secret value\n" VerifyXML = False Exit Function End If Set oVerifyKey = xmldsig.verify(oKey) If oVerifyKey Is Nothing Then Text1.Text = Text1.Text + "verify failed." + vbNewLine VerifyXML = False Exit Function End If Text1.Text = Text1.Text _ + "The data referenced in the signature object " _ + "was verified successfully." + vbNewLine + vbNewLine VerifyXML = True End Function Private Sub Form_Load() ' Resize the text box control to the size of the form Text1.Top = 100 Text1.Left = 100 Text1.Width = Form1.Width - 350 Text1.Height = Form1.Height - 750 DSIGNS = "xmlns:ds='http://www.w3.org/2000/09/xmldsig#'" sigTmp = "signature-template-enveloping-hmac-sha1.xml" sigOut = "signature-enveloping-hmac-sha1.xml" hmacSecret = "c2VjcmV0" hmacLength = -1 Text1.Text = "" If LoadXML(sigTmp) = False Then Exit Sub End If If SignXML(hmacSecret, hmacLength) = False Then Exit Sub End If If VerifyXML(hmacSecret, hmacLength) = False Then Exit Sub End If End Sub Private Sub Form_Resize() ' Resize the text box control to the size of the form Text1.Width = Form1.Width - 350 Text1.Height = Form1.Height - 750 End Sub
Try It!
- Ensure that you have completed all the procedures in Getting Started with XML Digital Signatures.
- Copy the signature template resource file, and paste it into a text editor. Save the file as signature-template-enveloping-hmac-sha1.xml.
- Create a Standard EXE project in Visual Basic. Save the empty project as KeyFromHMACSecret.vbp to the same directory where you saved signature-template-enveloping-hmac-sha1.xml. Name the form file KeyFromHMACSecret.frm.
- Create a reference to MSXML 5.0. To do this, select References... from the Project menu, and then check the box for Microsoft XML, v5.0.
- Copy the Visual Basic code listing above, and paste it into the code editor to replace whatever code is already there.
- Place a TextBox control onto the form. It should be named as Text1 (the default name). Click it to select it in the Properties window, and then modify the following properties for Text1: set the ScrollBars property to "2 – Vertical" and the MultiLine Property to True.
- Execute the code by selecting Start from the Run menu.
- Verify that the output is similar to that listed in the Output topic. You can resize the form to more easily view the output.