使用雜湊程式碼確定資料完整性

雜湊值是可唯一識別資料的固定長度數值。 雜湊值以較小的數值代表大量的資料,所以可和數位簽章一起使用。 比起簽署更大的值,簽署雜湊值更有效率。 如需驗證透過不安全的通道傳送的資料完整性,雜湊值會相當有用。 可比較已接收資料的雜湊值和該資料傳送時的雜湊值,來判斷資料是否已變更。

本主題描述如何使用 System.Security.Cryptography 命名空間中的類別來產生及驗證雜湊碼。

產生雜湊

雜湊類別可以雜湊位元組陣列或資料流物件。 以下範例使用 SHA-256 雜湊演算法來建立字串的雜湊值。 此範例會使用 Encoding.UTF8,將字串轉換成使用 SHA256 類別雜湊的位元組陣列。 接著會在主控台顯示雜湊值。

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

string messageString = "This is the original message!";

//Convert the string into an array of bytes.
byte[] messageBytes = Encoding.UTF8.GetBytes(messageString);

//Create the hash value from the array of bytes.
byte[] hashValue = SHA256.HashData(messageBytes);

//Display the hash value to the console.
Console.WriteLine(Convert.ToHexString(hashValue));
Imports System.Security.Cryptography
Imports System.Text

Module Program
    Sub Main()
        Dim messageString As String = "This is the original message!"

        'Convert the string into an array of bytes.
        Dim messageBytes As Byte() = Encoding.UTF8.GetBytes(messageString)

        'Create the hash value from the array of bytes.
        Dim hashValue As Byte() = SHA256.HashData(messageBytes)

        'Display the hash value to the console. 
        Console.WriteLine(Convert.ToHexString(hashValue))
    End Sub
End Module

此程式碼會在主控台顯示下列字串:

67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79

驗證雜湊

若要判斷資料完整性,可將資料和雜湊值相比較。 通常,資料會在特定時間進行雜湊,該雜湊值則會受某種方式保護。 在一段期間之後,可再重新雜湊資料,並與受保護的值相比較。 如果雜湊值相符,資料就未受變更。 如果值不相符,則資料已損毀。 為了讓這個系統運作,對於來自未受信任的所有對象,都必須將受保護的雜湊加密或保密。

下列範例會將字串的前一個雜湊值與新的雜湊值相比較。

using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

//This hash value is produced from "This is the original message!"
//using SHA256.
byte[] sentHashValue = Convert.FromHexString("67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79");

//This is the string that corresponds to the previous hash value.
string messageString = "This is the original message!";

//Convert the string into an array of bytes.
byte[] messageBytes = Encoding.UTF8.GetBytes(messageString);

//Create the hash value from the array of bytes.
byte[] compareHashValue = SHA256.HashData(messageBytes);

//Compare the values of the two byte arrays.
bool same = sentHashValue.SequenceEqual(compareHashValue);

//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.");
}
Imports System.Linq
Imports System.Security.Cryptography
Imports System.Text

Module Module1
    Sub Main()
        'This hash value is produced from "This is the original message!" 
        'using SHA256.  
        Dim sentHashValue As Byte() = Convert.FromHexString("67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79")

        'This is the string that corresponds to the previous hash value.
        Dim messageString As String = "This is the original message!"

        'Convert the string into an array of bytes.
        Dim messageBytes As Byte() = Encoding.UTF8.GetBytes(messageString)

        'Create the hash value from the array of bytes.
        Dim compareHashValue As Byte() = SHA256.HashData(messageBytes)

        'Compare the values of the two byte arrays.
        Dim same As Boolean = sentHashValue.SequenceEqual(compareHashValue)

        '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

另請參閱