MD5CryptoServiceProvider クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
ハッシュ関数は、任意の長さのバイナリ文字列を固定長の小さなバイナリ文字列に割り当てます。暗号ハッシュ関数では、同じ値にハッシュされる 2 つの異なる入力値を検出することが計算上不可能です。つまり、2 組のデータのハッシュは、対応するデータも一致している場合にだけ一致します。データを少し変更しただけでも、ハッシュは予測できないほど大幅に変更されてしまいます。
MD5CryptoServiceProvider クラスのハッシュ サイズは 128 ビットです。
MD5CryptoServiceProvider クラスの ComputeHash メソッドは、16 バイトの配列としてハッシュを返します。一部の MD5 実装では、32 文字の 16 進形式のハッシュを生成します。このような実装と相互運用するには、ComputeHash メソッドの戻り値を 16 進値として形式指定します。
data の MD5 ハッシュ値を計算して返すコード例を次に示します。
Function MD5hash(data() As Byte) As Byte() ' This is one implementation of the abstract class MD5. Dim md5 As New MD5CryptoServiceProvider() Dim result As Byte() = md5.ComputeHash(data) Return result End Function
byte[] MD5hash (byte[] data) { // This is one implementation of the abstract class MD5. MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(data); return result; }
private: array<Byte>^ MD5hash( array<Byte>^data ) { // This is one implementation of the abstract class MD5. MD5^ md5 = gcnew MD5CryptoServiceProvider; array<Byte>^ result = md5->ComputeHash( data ); return result; }
ubyte[] MD5hash(ubyte data[]) { // This is one implementation of the abstract class MD5. MD5 md5 = new MD5CryptoServiceProvider(); ubyte result[] = md5.ComputeHash(data); return result; } //MD5hash
文字列の MD5 ハッシュ値を計算し、32 文字の 16 進形式の文字列としてハッシュを返すコード例を次に示します。このコード例で作成されるハッシュ文字列は、32 文字の 16 進形式のハッシュ文字列を作成する (任意のプラットフォーム上の) MD5 ハッシュ関数と互換性があります。
Imports System Imports System.Security.Cryptography Imports System.Text Module Example ' Hash an input string and return the hash as ' a 32 character hexadecimal string. Function getMd5Hash(ByVal input As String) As String ' Create a new instance of the MD5CryptoServiceProvider object. Dim md5Hasher As New MD5CryptoServiceProvider() ' Convert the input string to a byte array and compute the hash. Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)) ' Create a new Stringbuilder to collect the bytes ' and create a string. Dim sBuilder As New StringBuilder() ' Loop through each byte of the hashed data ' and format each one as a hexadecimal string. Dim i As Integer For i = 0 To data.Length - 1 sBuilder.Append(data(i).ToString("x2")) Next i ' Return the hexadecimal string. Return sBuilder.ToString() End Function ' Verify a hash against a string. Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean ' Hash the input. Dim hashOfInput As String = getMd5Hash(input) ' Create a StringComparer an comare the hashes. Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase If 0 = comparer.Compare(hashOfInput, hash) Then Return True Else Return False End If End Function Sub Main() Dim source As String = "Hello World!" Dim hash As String = getMd5Hash(source) Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".") Console.WriteLine("Verifying the hash...") If verifyMd5Hash(source, hash) Then Console.WriteLine("The hashes are the same.") Else Console.WriteLine("The hashes are not same.") End If End Sub End Module ' This code example produces the following output: ' ' The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c. ' Verifying the hash... ' The hashes are the same.
using System; using System.Security.Cryptography; using System.Text; class Example { // Hash an input string and return the hash as // a 32 character hexadecimal string. static string getMd5Hash(string input) { // Create a new instance of the MD5CryptoServiceProvider object. MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } // Verify a hash against a string. static bool verifyMd5Hash(string input, string hash) { // Hash the input. string hashOfInput = getMd5Hash(input); // Create a StringComparer an comare the hashes. StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (0 == comparer.Compare(hashOfInput, hash)) { return true; } else { return false; } } static void Main() { string source = "Hello World!"; string hash = getMd5Hash(source); Console.WriteLine("The MD5 hash of " + source + " is: " + hash + "."); Console.WriteLine("Verifying the hash..."); if (verifyMd5Hash(source, hash)) { Console.WriteLine("The hashes are the same."); } else { Console.WriteLine("The hashes are not same."); } } } // This code example produces the following output: // // The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c. // Verifying the hash... // The hashes are the same.
System.Security.Cryptography.HashAlgorithm
System.Security.Cryptography.MD5
System.Security.Cryptography.MD5CryptoServiceProvider
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- MD5CryptoServiceProvider クラスのページへのリンク