title: The correct way to encrypt a string using HmacSHA256 in VB.NET
body: <p>I need to create a keyed hash for a string of XML to send to a 3rd party. This is the code I am using but it is producing a different hash than the example that the 3rd party has sent me. I have been through all the tutorials I can find and re-read MSDN again and again. What am I doing wrong? Or should I suspect a problem at the other end?</p>
  
  <pre><code>Public Shared Function HashString(ByVal StringToHash As String) As String
      Dim myEncoder As New System.Text.UTF32Encoding
      Dim Key() As Byte = myEncoder.GetBytes(My.Settings.PortalHASH)
      Dim XML() As Byte = myEncoder.GetBytes(StringToHash)
      Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
      Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
      Return Convert.ToBase64String(HashCode)
  End Function
  </code></pre>
  
  <p>It needs to be base-64 encoded, which is why I have the last line.</p>
  
  <p>Thanks</p>