Freigeben über


MD5-Klasse

Stellt die abstrakte Klasse dar, von der alle Implementierungen des MD5-Hashalgorithmus vererbt werden.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public MustInherit Class MD5
    Inherits HashAlgorithm
'Usage
Dim instance As MD5
[ComVisibleAttribute(true)] 
public abstract class MD5 : HashAlgorithm
[ComVisibleAttribute(true)] 
public ref class MD5 abstract : public HashAlgorithm
/** @attribute ComVisibleAttribute(true) */ 
public abstract class MD5 extends HashAlgorithm
ComVisibleAttribute(true) 
public abstract class MD5 extends HashAlgorithm

Hinweise

Hashfunktionen ordnen binäre Zeichenfolgen beliebiger Länge kleinen binären Zeichenfolgen fester Länge zu. Kryptografische Hashfunktionen sind dadurch gekennzeichnet, dass es rechnerisch unmöglich ist, zwei verschiedene Eingabewerte zu ermitteln, die denselben Hashwert ergeben. Daher sind die Hashs zweier Datenmengen identisch, wenn auch die entsprechenden Daten identisch sind. Kleine Änderungen an den Daten führen zu beträchtlichen unvorhersehbaren Änderungen des Hash.

Die Hashgröße für den MD5-Algorithmus beträgt 128 Bit.

Die ComputeHash-Methoden der MD5-Klasse geben den Hash als 16-Byte-Array zurück. Beachten Sie, dass einige MD5-Implementierungen einen Hash in Hexadezimalformat mit einer Länge von 32 Zeichen erzeugen. Bei der Interaktion mit den betreffenden Implementierungen sollten Sie den Rückgabewert der ComputeHash-Methoden als Hexadezimalwert formatieren.

Beispiel

Im folgenden Codebeispiel wird der MD5-Hashwert für eine Zeichenfolge berechnet und der Hash als Zeichenfolge in Hexadezimalformat mit einer Länge von 32 Zeichen zurückgegeben. Die in diesem Codebeispiel erstellte Hashzeichenfolge ist mit allen MD5-Hashfunktionen (auf allen Plattformen) kompatibel, bei denen eine Hashzeichenfolge in Hexadezimalformat mit einer Länge von 32 Zeichen erzeugt wird.

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 MD5 object.
        Dim md5Hasher As MD5 = MD5.Create()

        ' 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.
        MD5 md5Hasher = MD5.Create();

        // 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.

Vererbungshierarchie

System.Object
   System.Security.Cryptography.HashAlgorithm
    System.Security.Cryptography.MD5
       System.Security.Cryptography.MD5CryptoServiceProvider

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

MD5-Member
System.Security.Cryptography-Namespace

Weitere Ressourcen

Kryptografische Dienste