Share via


ハッシュ コードによるデータの整合性の保証

ハッシュ値とは、データを一意に識別する固定長の数値です。 ハッシュ値は、大量のデータを非常に小さな数値として表現するため、デジタル署名と一緒に使用されます。 大きな値を署名するより、ハッシュ値によって効率的に署名できます。 また、ハッシュ値は、安全でないチャネルをとおして送信されたデータの整合性を検査する場合にも役立ちます。 受信データのハッシュ値を送信前のデータのハッシュ値と比較すると、データが変更されたかどうかを判断できます。

このトピックでは、System.Security.Cryptography 名前空間のクラスを使用してハッシュ コードを生成したり検査したりする方法について説明します。

ハッシュの生成

マネージ ハッシュ クラスでは、バイト配列またはマネージ ストリーム オブジェクトのいずれかをハッシュできます。 SHA1 ハッシュ アルゴリズムを使用して、文字列のハッシュ値を作成する例を次に示します。 この例では、UnicodeEncoding クラスを使用して文字列をバイト配列に変換し、SHA1Managed クラスを使用してバイト配列をハッシュします。 ハッシュ値はコンソールに表示されます。

Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Program
    Sub Main()
        Dim HashValue() As Byte

        Dim MessageString As String = "This is the original message!"

        'Create a new instance of the UnicodeEncoding class to 
        'convert the string into an array of Unicode bytes.
        Dim UE As New UnicodeEncoding()

        'Convert the string into an array of bytes.
        Dim MessageBytes As Byte() = UE.GetBytes(MessageString)

        'Create a new instance of the SHA1Managed class to create 
        'the hash value.
        Dim SHhash As New SHA1Managed()

        'Create the hash value from the array of bytes.
        HashValue = SHhash.ComputeHash(MessageBytes)

        'Display the hash value to the console. 
        Dim b As Byte
        For Each b In HashValue
            Console.Write("{0} ", b)
        Next b
    End Sub
End Module
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class Class1
{
    static void Main(string[] args)
    {
        byte[] HashValue;

        string MessageString = "This is the original message!";

        //Create a new instance of the UnicodeEncoding class to 
        //convert the string into an array of Unicode bytes.
        UnicodeEncoding UE = new UnicodeEncoding();

        //Convert the string into an array of bytes.
        byte[] MessageBytes = UE.GetBytes(MessageString);

        //Create a new instance of the SHA1Managed class to create 
        //the hash value.
        SHA1Managed SHhash = new SHA1Managed();

        //Create the hash value from the array of bytes.
        HashValue = SHhash.ComputeHash(MessageBytes);

        //Display the hash value to the console. 
        foreach (byte b in HashValue)
        {
            Console.Write("{0} ", b);
        }
    }
}

上記のコードは、次の文字列をコンソールに表示します。

59 4 248 102 77 97 142 201 210 12 224 93 25 41 100 197 213 134 130 135

ハッシュの検査

データをハッシュ値と比較すると、整合性を判断できます。 通常、データは特定のタイミングでハッシュされ、ハッシュ値はなんらかの方法で保護されます。 後からデータをもう一度ハッシュして、保護されている値と比較できます。 ハッシュ値が一致する場合、データは変更されていません。 値が一致しない場合はデータが破損しています。 このシステムを機能させるためには、保護されているハッシュを暗号化するか、または信頼していない人に対して秘密にしておく必要があります。

文字列の以前のハッシュ値を新しいハッシュ値と比較する例を次に示します。 この例では、ループを使用してハッシュ値の各バイトを比較します。

Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Module1
    Sub Main()
        'This hash value is produced from "This is the original message!" 
        'using SHA1Managed.  
        Dim SentHashValue As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}

        'This is the string that corresponds to the previous hash value.
        Dim MessageString As String = "This is the original message!"

        Dim CompareHashValue() As Byte

        'Create a new instance of the UnicodeEncoding class to 
        'convert the string into an array of Unicode bytes.
        Dim UE As New UnicodeEncoding()

        'Convert the string into an array of bytes.
        Dim MessageBytes As Byte() = UE.GetBytes(MessageString)

        'Create a new instance of the SHA1Managed class to create 
        'the hash value.
        Dim SHhash As New SHA1Managed()

        'Create the hash value from the array of bytes.
        CompareHashValue = SHhash.ComputeHash(MessageBytes)

        Dim Same As Boolean = True

        'Compare the values of the two byte arrays.
        Dim x As Integer
        For x = 0 To SentHashValue.Length - 1
            If SentHashValue(x) <> CompareHashValue(x) Then
                Same = False
            End If
        Next x
        '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
using System;
using System.Security.Cryptography;
using System.Text;

class Class1
{
    static void Main()
    {
        //This hash value is produced from "This is the original message!" 
        //using SHA1Managed.  
        byte[] SentHashValue = { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 };

        //This is the string that corresponds to the previous hash value.
        string MessageString = "This is the original message!";

        byte[] CompareHashValue;

        //Create a new instance of the UnicodeEncoding class to 
        //convert the string into an array of Unicode bytes.
        UnicodeEncoding UE = new UnicodeEncoding();

        //Convert the string into an array of bytes.
        byte[] MessageBytes = UE.GetBytes(MessageString);

        //Create a new instance of the SHA1Managed class to create 
        //the hash value.
        SHA1Managed SHhash = new SHA1Managed();

        //Create the hash value from the array of bytes.
        CompareHashValue = SHhash.ComputeHash(MessageBytes);

        bool Same = true;

        //Compare the values of the two byte arrays.
        for (int x = 0; x < SentHashValue.Length; x++)
        {
            if (SentHashValue[x] != CompareHashValue[x])
            {
                Same = false;
            }
        }
        //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.");
        }
    }
}

2 つのハッシュ値が一致する場合は、上記のコードによってコンソールに次のメッセージが表示されます。

The hash codes match.

一致しない場合は次のメッセージが表示されます。

The hash codes do not match.

参照

概念

暗号サービス

その他の技術情報

暗号タスク