使用哈希代码确保数据完整性
哈希值是唯一地标识数据的固定长度的数字值。 哈希值可将大量的数据表示为小得多的数值,因而应用于数字签名。 使用哈希值进行签名比使用更大的值进行签名更有效。 哈希值还可用于验证通过不安全信道发送的数据的完整性。 可以将接收到的数据的哈希值与数据在发送时的哈希值进行比较,以确定该数据是否已被更改。
本主题介绍如何使用 System.Security.Cryptography 命名空间中的类生成和验证哈希代码。
生成哈希
托管哈希类可以散列字节数组或托管流对象。 下面的示例使用 SHA1 哈希算法创建字符串的哈希值。 该示例使用 UnicodeEncoding 类将字符串转换为使用 SHA1Managed 类进行散列处理的字节数组。 然后将该哈希值显示到控制台。
Imports System
Imports System.Security.Cryptography
Imports System.Text
Module Program
Sub Main()
Dim HashValue() As Byte
Dim MessageString As String = "This is the original message!"
'Create a new instance of the UnicodeEncoding class to
'convert the string into an array of Unicode bytes.
Dim UE As New UnicodeEncoding()
'Convert the string into an array of bytes.
Dim MessageBytes As Byte() = UE.GetBytes(MessageString)
'Create a new instance of the SHA1Managed class to create
'the hash value.
Dim SHhash As New SHA1Managed()
'Create the hash value from the array of bytes.
HashValue = SHhash.ComputeHash(MessageBytes)
'Display the hash value to the console.
Dim b As Byte
For Each b In HashValue
Console.Write("{0} ", b)
Next b
End Sub
End Module
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class Class1
{
static void Main(string[] args)
{
byte[] HashValue;
string MessageString = "This is the original message!";
//Create a new instance of the UnicodeEncoding class to
//convert the string into an array of Unicode bytes.
UnicodeEncoding UE = new UnicodeEncoding();
//Convert the string into an array of bytes.
byte[] MessageBytes = UE.GetBytes(MessageString);
//Create a new instance of the SHA1Managed class to create
//the hash value.
SHA1Managed SHhash = new SHA1Managed();
//Create the hash value from the array of bytes.
HashValue = SHhash.ComputeHash(MessageBytes);
//Display the hash value to the console.
foreach (byte b in HashValue)
{
Console.Write("{0} ", b);
}
}
}
上面的代码将把下面的字符串显示到控制台:
59 4 248 102 77 97 142 201 210 12 224 93 25 41 100 197 213 134 130 135
验证哈希
可以将数据与哈希值进行比较以确定数据的完整性。 通常,在某个时刻散列数据并以某种方式保护哈希值。 可以在以后再次散列该数据并与被保护的值进行比较。 如果哈希值匹配,则表示数据未被更改。 如果哈希值不匹配,则表示数据已被破坏。 为使此系统发挥作用,必须对受保护的哈希值进行加密或将该哈希值对所有不受信任的用户保密。
下面的示例将字符串以前的哈希值与新的哈希值进行比较。 本示例遍历哈希值的每个字节并进行比较。
Imports System
Imports System.Security.Cryptography
Imports System.Text
Module Module1
Sub Main()
'This hash value is produced from "This is the original message!"
'using SHA1Managed.
Dim SentHashValue As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}
'This is the string that corresponds to the previous hash value.
Dim MessageString As String = "This is the original message!"
Dim CompareHashValue() As Byte
'Create a new instance of the UnicodeEncoding class to
'convert the string into an array of Unicode bytes.
Dim UE As New UnicodeEncoding()
'Convert the string into an array of bytes.
Dim MessageBytes As Byte() = UE.GetBytes(MessageString)
'Create a new instance of the SHA1Managed class to create
'the hash value.
Dim SHhash As New SHA1Managed()
'Create the hash value from the array of bytes.
CompareHashValue = SHhash.ComputeHash(MessageBytes)
Dim Same As Boolean = True
'Compare the values of the two byte arrays.
Dim x As Integer
For x = 0 To SentHashValue.Length - 1
If SentHashValue(x) <> CompareHashValue(x) Then
Same = False
End If
Next x
'Display whether or not the hash values are the same.
If Same Then
Console.WriteLine("The hash codes match.")
Else
Console.WriteLine("The hash codes do not match.")
End If
End Sub
End Module
using System;
using System.Security.Cryptography;
using System.Text;
class Class1
{
static void Main()
{
//This hash value is produced from "This is the original message!"
//using SHA1Managed.
byte[] SentHashValue = { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 };
//This is the string that corresponds to the previous hash value.
string MessageString = "This is the original message!";
byte[] CompareHashValue;
//Create a new instance of the UnicodeEncoding class to
//convert the string into an array of Unicode bytes.
UnicodeEncoding UE = new UnicodeEncoding();
//Convert the string into an array of bytes.
byte[] MessageBytes = UE.GetBytes(MessageString);
//Create a new instance of the SHA1Managed class to create
//the hash value.
SHA1Managed SHhash = new SHA1Managed();
//Create the hash value from the array of bytes.
CompareHashValue = SHhash.ComputeHash(MessageBytes);
bool Same = true;
//Compare the values of the two byte arrays.
for (int x = 0; x < SentHashValue.Length; x++)
{
if (SentHashValue[x] != CompareHashValue[x])
{
Same = false;
}
}
//Display whether or not the hash values are the same.
if (Same)
{
Console.WriteLine("The hash codes match.");
}
else
{
Console.WriteLine("The hash codes do not match.");
}
}
}
如果两个哈希值匹配,则上面的代码将把以下内容显示到控制台:
The hash codes match.
如果两个哈希值不匹配,代码将显示下面的内容:
The hash codes do not match.