次の方法で共有


ハッシュの生成

更新 : 2007 年 11 月

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

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

Module Module1
    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

参照

概念

ハッシュの検査

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

その他の技術情報

暗号タスク

暗号サービス