Hashing is a reproducible process to calculate a hash value from some data. The hash values are unique and specific for the data it was derived from. Wikipedia has a good explanation of hashing. i will be showing some VB.Net code in this article that uses the SHA1 algorithm to do some hashing.
Hashing by itself does nothing to prevent tampering with the data. Message Authentication Codes (MAC) are a way to prevent this. MACs use symmetric encryption methods to protect the sent hash. Symmetric encryption uses one private session key and both the sender and receiver require to have a copy of this key.
Below is the VB.Net code to generate a MAC using HMACSHA1:
1: Imports System.Security.Cryptography
2:
3: Public Shared Function ComputeHash(ByVal Data As String, ByVal Key As String) As String
4: ' initialize the keyed hash
5: Dim sha1Algo As New HMACSHA1(System.Text.Encoding.ASCII.GetBytes(Key))
6:
7: ' compute hash
8: Dim hash As Byte() = sha1Algo.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data))
9:
10: ' convert to string and return
11: Return System.BitConverter.ToString(hash)
12: End Function
13:
14: Public Shared Function VerifyHash(ByVal Received As String, ByVal Data As String, ByVal Key As String) As Boolean
15: ' hash our data
16: Dim hash as String = ComputeHash(Data, Key)
17:
18: ' compare strings
19: Return (Received = hash)
20: End Function
21:
This code was derived from a more in-depth article: Hashing, MACs, and Digital Signatures in .NET.