Karma Kodlarla Veri Bütünlüğünü Sağlama

Karma değer, verileri benzersiz olarak tanımlayan sabit uzunlukta bir sayısal değerdir. Karma değerler, büyük miktarlardaki verileri çok daha küçük sayısal değerler olarak temsil eder, bu nedenle dijital imzalarla kullanılırlar. Karma değeri, daha büyük değeri imzalamaktan daha verimli bir şekilde imzalayabilirsiniz. Karma değerler, güvenli olmayan kanallardan gönderilen verilerin bütünlüğünü doğrulamak için de yararlıdır. Alınan verilerin karma değeri, verilerin değiştirilip değiştirilmediğini belirlemek için gönderildikçe verilerin karma değeriyle karşılaştırılabilir.

Bu konu başlığında, ad alanında System.Security.Cryptography sınıfları kullanarak karma kodların nasıl oluşturulacağı ve doğrulanacağı açıklanmaktadır.

Karma Oluşturma

Karma sınıfları bir bayt dizisini veya akış nesnesini karma olarak kullanabilir. Aşağıdaki örnekte sha-256 karma algoritması bir dize için karma değeri oluşturmak için kullanılır. Örnek, dizeyi sınıfı kullanılarak karma haline getirilmiş bir bayt dizisine SHA256 dönüştürmek için kullanırEncoding.UTF8. Karma değer daha sonra konsolda görüntülenir.

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

Bu kod konsolda aşağıdaki dizeyi görüntüler:

67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79

Karma Doğrulama

Veriler, bütünlüğünü belirlemek için karma değerle karşılaştırılabilir. Genellikle veriler belirli bir zamanda karma hale getirilir ve karma değeri bir şekilde korunur. Daha sonra veriler yeniden karmalanabilir ve korunan değerle karşılaştırılabilir. Karma değerler eşleşiyorsa veriler değiştirilmemiştir. Değerler eşleşmiyorsa veriler bozulmuştur. Bu sistemin çalışması için korumalı karma şifrelenmeli veya tüm güvenilmeyen taraflardan gizli tutulmalıdır.

Aşağıdaki örnek, bir dizenin önceki karma değerini yeni bir karma değerle karşılaştırır.

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

Ayrıca bkz.