HOW TO: Compute and Compare Hash Values Using Visual Basic .NET
点击次数:49 次 发布日期:2008-11-06 08:07:12 作者:源代码网
|
源代码网推荐 源代码网推荐 -------------------------------------------------------------------------------- 源代码网推荐 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). 源代码网推荐 源代码网供稿. |
