更新:2007 年 11 月
使用密碼編譯服務提供者 (CSP) 所提供之實作,計算輸入資料的 MD5 雜湊值。這個類別無法被繼承。
命名空間:
System.Security.Cryptography 組件:
mscorlib (在 mscorlib.dll 中)
<ComVisibleAttribute(True)> _
Public NotInheritable Class MD5CryptoServiceProvider _
Inherits MD5
Dim instance As MD5CryptoServiceProvider
[ComVisibleAttribute(true)]
public sealed class MD5CryptoServiceProvider : MD5
[ComVisibleAttribute(true)]
public ref class MD5CryptoServiceProvider sealed : public MD5
/** @attribute ComVisibleAttribute(true) */
public final class MD5CryptoServiceProvider extends MD5
public final class MD5CryptoServiceProvider extends MD5
雜湊函式會將任意長度的二進位字串對應到固定長度的小型二進位字串。密碼編譯雜湊函式具有一屬性,即在計算上不可能找到兩個不同的輸入能雜湊為相同的值;也就是說,如果對應的資料相符,兩個資料組的雜湊應會相符。資料的些微變更會造成雜湊中無法預期的大量變更。
MD5CryptoServiceProvider 類別的雜湊大小是 128 位元。
MD5CryptoServiceProvider 類別的 ComputeHash 方法會以 16 位元組陣列傳回雜湊。請注意,一些 MD5 實作會產生 32 字元的十六進位格式雜湊。若要與這樣的實作互通,請將 ComputeHash 方法的傳回值格式化為十六進位值。
下列程式碼範例會計算 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 雜湊值 (Hash Value),並以 32 字元十六進位格式的字串傳回雜湊。這個程式碼範例建立的雜湊字串,與任何建立 32 字元十六進位格式之雜湊字串的 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 compare 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 compare 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..::.Object
System.Security.Cryptography..::.HashAlgorithm
System.Security.Cryptography..::.MD5
System.Security.Cryptography..::.MD5CryptoServiceProvider
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求。
.NET Framework
支援版本:3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
支援版本:3.5、2.0
參考
其他資源