日期:2009-02-01  浏览次数:20345 次

HOW TO: Compute and Compare Hash Values Using Visual Basic .NET

--------------------------------------------------------------------------------

The information in this article applies to:

Microsoft Visual Basic .NET Beta 2

--------------------------------------------------------------------------------
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about obtaining support for a Beta release, please see the documentation included with the Beta product files, or check the Web location from which you downloaded the release.

For a Microsoft C# .NET version of this article, see Q307020 .

IN THIS TASK
SUMMARY
Requirements
Compute a Hash Value
Compare Two Hash Values
Complete Code Listing
REFERENCES


SUMMARY
The System.Security.Cryptography classes in the Microsoft .NET Framework make it easy to compute a hash value for your source data. This article shows how to obtain a hash value and how to compare two hash values to check whether they are identical.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
Microsoft Visual Studio .NET


back to the top
Compute a Hash Value
It is easy to generate and compare hash values using the cryptographic resources contained in the System.Security.Cryptography namespace. Because all hash functions take input of type Byte[] , it might be necessary to convert the source into a byte array before it is hashed. To create a hash for a string value, follow these steps:
Open Visual Studio .NET.


Create a new Console Application in Visual Basic .NET. Visual Studio .NET creates a Module for you along with an empty Main() procedure.


Make sure that the project references the System and System.Security namespaces.


Use the Imports statement on the System , System.Security , System.Security.Cryptographic , and System.Text namespaces so that you are not required to qualify declarations from these namespaces later in your code. These statements must be used prior to any other declarations.


Imports System
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Declare a string variable to hold your source data, and two byte arrays (of undefined size) to hold the source bytes and the resulting hash value.


Dim sSourceData As String
Dim tmpSource() As Byte
Dim tmpHash() As Byte
Use the GetBytes() function, which is part of the System.Text.ASCIIEncoding.ASCII class, to convert your source string into an array of bytes (required as input to the hashing function).


sSourceData = "MySourceData"
'Create a byte array from source data.
tmpSource = ASCIIEncoding.ASCII.GetBytes(sSourceData)
Compute the MD5 hash for your source data by calling ComputeHash on an instance of the MD5CryptoServiceProvider class. Note that to compute another hash value, you will need to create another instance of the class.


'Compute hash based on source data.
tmpHash = New MD5CryptoServiceProvider().ComputeHash(tmpSource)
The tmpHash byte array now holds the computed hash value (128-bit value=16 bytes) for your source data. It is often useful to display or store a value like this as a hexadecimal string, which the following code accomplishes:


Console.WriteLine(ByteArrayToS