Sdílet prostřednictvím


Zajištění integrity dat pomocí hodnot hash

Hodnota hash je číselná hodnota pevné délky, která jedinečně identifikuje data. Hodnoty hash představují velké objemy dat jako mnohem menší číselné hodnoty, takže se používají s digitálními podpisy. Hodnotu hash můžete podepsat efektivněji, než když podepíšete větší hodnotu. Hodnoty hash jsou také užitečné pro ověření integrity dat odesílaných prostřednictvím nezabezpečených kanálů. Hodnotu hash přijatých dat je možné porovnat s hodnotou hash dat, která byla odeslána, a určit, zda byla data změněna.

Toto téma popisuje, jak generovat a ověřit kódy hash pomocí tříd v System.Security.Cryptography oboru názvů.

Generování hodnoty hash

Třídy hash mohou hashovat buď pole bajtů, nebo stream objektu. Následující příklad používá algoritmus hash SHA-256 k vytvoření hodnoty hash pro řetězec. Příklad používá Encoding.UTF8 k převodu řetězce na pole bajtů, které jsou hashovány pomocí SHA256 třídy. Hodnota hash se pak zobrazí v konzole.

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

Tento kód zobrazí v konzole následující řetězec:

67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79

Ověření hodnoty hash

Data je možné porovnat s hodnotou hash a určit její integritu. Data jsou obvykle v určitém okamžiku hashována a hodnota hash je chráněna nějakým způsobem. Později je možné data znovu zatřiďovat a porovnat s chráněnou hodnotou. Pokud se hodnoty hash shodují, data nebyla změněna. Pokud se hodnoty neshodují, data byla poškozena. Aby tento systém fungoval, musí být chráněná hodnota hash zašifrována nebo udržována v tajnosti od všech nedůvěryhodných stran.

Následující příklad porovnává předchozí hodnotu hash řetězce s novou hodnotou hash.

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

Viz také