Udostępnij za pośrednictwem


Zapewnianie integralności danych za pomocą wartości skrótu

Wartość skrótu to wartość liczbowa o stałej długości, która jednoznacznie identyfikuje dane. Wartości skrótu reprezentują duże ilości danych tyle mniejszych wartości liczbowych, aby były używane z podpisami cyfrowymi. Wartość skrótu można podpisać wydajniej niż podpisywanie większej wartości. Wartości skrótu są również przydatne do weryfikowania integralności danych wysyłanych za pośrednictwem niezabezpieczonych kanałów. Wartość skrótu odebranych danych może być porównywana z wartością skrótu danych, ponieważ została wysłana w celu ustalenia, czy dane zostały zmienione.

W tym temacie opisano sposób generowania i weryfikowania kodów skrótów przy użyciu klas w System.Security.Cryptography przestrzeni nazw.

Generowanie skrótu

Klasy skrótu mogą zawierać skrót tablicy bajtów lub obiektu strumienia. W poniższym przykładzie użyto algorytmu wyznaczania wartości skrótu SHA-256 w celu utworzenia wartości skrótu dla ciągu. W przykładzie użyto Encoding.UTF8 metody , aby przekonwertować ciąg na tablicę bajtów, które są skrótami SHA256 przy użyciu klasy . Wartość skrótu jest następnie wyświetlana w konsoli programu .

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

Ten kod wyświetli następujący ciąg w konsoli:

67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79

Weryfikowanie skrótu

Dane można porównać z wartością skrótu, aby określić jego integralność. Zazwyczaj dane są szyfrowane w określonym czasie, a wartość skrótu jest w jakiś sposób chroniona. W późniejszym czasie dane mogą zostać ponownie skrócone i porównane z wartością chronioną. Jeśli wartości skrótu są zgodne, dane nie zostały zmienione. Jeśli wartości nie są zgodne, dane zostały uszkodzone. Aby ten system działał, chroniony skrót musi być zaszyfrowany lub tajny ze wszystkich niezaufanych stron.

Poniższy przykład porównuje poprzednią wartość skrótu ciągu z nową wartością skrótu.

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

Zobacz też