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!