Megosztás a következőn keresztül:


Adatintegritás biztosítása kivonatkódokkal

A kivonatérték egy rögzített hosszúságú numerikus érték, amely egyedileg azonosítja az adatokat. A kivonatértékek nagy mennyiségű adatot jelölnek, mint sokkal kisebb numerikus értékeket, ezért digitális aláírásokkal használják őket. A kivonatértékeket hatékonyabban írhatja alá, mint a nagyobb érték aláírását. A kivonatértékek a nem biztonságos csatornákon keresztül küldött adatok integritásának ellenőrzéséhez is hasznosak. A fogadott adatok kivonatértéke összehasonlítható az elküldött adatok kivonatértékével annak megállapításához, hogy az adatok módosultak-e.

Ez a témakör azt ismerteti, hogyan hozhat létre és ellenőrizhet kivonatkódokat a System.Security.Cryptography névtérben lévő osztályok használatával.

Kivonat létrehozása

A kivonatosztályok bájtból vagy streamobjektumból álló tömböt is kivonatozhatnak. Az alábbi példa az SHA-256 kivonatoló algoritmus használatával hoz létre kivonatértéket egy sztringhez. A példa Encoding.UTF8 a sztringet bájtok tömbjeként alakítja át, amelyeket az SHA256 osztály használ. Ekkor megjelenik a kivonat értéke a konzolon.

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

Ez a kód a következő sztringet jeleníti meg a konzolon:

67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79

Kivonat ellenőrzése

Az adatok összehasonlíthatók kivonatértékel az integritás meghatározásához. Az adatok kivonata általában egy adott időpontban történik, és a kivonat értéke valamilyen módon védett. Később az adatok ismét kivonatolódhatnak, és összehasonlíthatók a védett értékkel. Ha a kivonat értékei egyeznek, az adatok nem módosultak. Ha az értékek nem egyeznek, az adatok sérültek. Ahhoz, hogy ez a rendszer működjön, a védett kivonatot titkosítani vagy titkosítani kell az összes nem megbízható féltől.

Az alábbi példa egy sztring előző kivonatértékét hasonlítja össze egy új kivonatértékel.

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

Lásd még