Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Nilai hash adalah nilai numerik dengan panjang tetap yang secara unik mengidentifikasi data. Nilai hash mewakili sejumlah besar data sebagai nilai numerik yang jauh lebih kecil, sehingga digunakan dengan tanda tangan digital. Anda dapat menandatangani nilai hash dengan lebih efisien daripada menandatangani nilai yang lebih besar. Nilai hash juga berguna untuk memverifikasi integritas data yang dikirim melalui saluran yang tidak aman. Nilai hash data yang diterima dapat dibandingkan dengan nilai hash data saat dikirim untuk menentukan jika data diubah.
Topik ini menjelaskan cara membuat dan memverifikasi kode hash dengan menggunakan kelas di kumpulan nama XML System.Security.Cryptography.
Menghasilkan Hash
Kelas hash dapat hash baik array byte atau objek stream. Contoh berikut menggunakan algoritma hash SHA-256 untuk membuat nilai hash untuk string. Contoh menggunakan Encoding.UTF8 untuk mengonversi string menjadi array byte yang di-hash dengan menggunakan SHA256 kelas . Nilai hash kemudian ditampilkan ke konsol.
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
Kode ini akan menampilkan string berikut ke konsol:
67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79
Memverifikasi Hash
Data dapat dibandingkan dengan nilai hash untuk menentukan integritasnya. Biasanya, data di-hash pada waktu tertentu dan nilai hash dilindungi dalam beberapa cara. Di lain waktu, data dapat di-hash lagi dan dibandingkan dengan nilai yang dilindungi. Jika nilai hash cocok, data belum diubah. Jika nilai tidak cocok, data telah rusak. Agar sistem ini berfungsi, hash yang dilindungi harus dienkripsi atau dirahasiakan dari semua pihak yang tidak tepercaya.
Contoh berikut membandingkan nilai hash string sebelumnya dengan nilai hash baru.
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