當前位置: 首頁>>代碼示例>>C#>>正文


C# MD5CryptoServiceProvider類代碼示例

本文整理匯總了C#中System.Security.Cryptography.MD5CryptoServiceProvider的典型用法代碼示例。如果您正苦於以下問題:C# MD5CryptoServiceProvider類的具體用法?C# MD5CryptoServiceProvider怎麽用?C# MD5CryptoServiceProvider使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MD5CryptoServiceProvider類屬於System.Security.Cryptography命名空間,在下文中一共展示了MD5CryptoServiceProvider類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: MD5hash

byte[] MD5hash (byte[] data)
 {
    // This is one implementation of the abstract class MD5.
    MD5 md5 = new MD5CryptoServiceProvider();

    byte[] result = md5.ComputeHash(data);

    return result;
 }
開發者ID:.NET開發者,項目名稱:System.Security.Cryptography,代碼行數:9,代碼來源:MD5CryptoServiceProvider

示例2: getMd5Hash

//引入命名空間
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.");
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Security.Cryptography,代碼行數:71,代碼來源:MD5CryptoServiceProvider

輸出:

The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c.
Verifying the hash...
The hashes are the same.


注:本文中的System.Security.Cryptography.MD5CryptoServiceProvider類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。