C#計算字符串哈希值(MD5、SHA)的方法小結(jié)
本文實例講述了C#計算字符串哈希值(MD5、SHA)的方法。分享給大家供大家參考。具體如下:
一、關(guān)于本文
本文中是一個類庫,包括下面幾個函數(shù):
① 計算32位MD5碼(大小寫):Hash_MD5_32
② 計算16位MD5碼(大小寫):Hash_MD5_16
③ 計算32位2重MD5碼(大小寫):Hash_2_MD5_32
④ 計算16位2重MD5碼(大小寫):Hash_2_MD5_16
⑤ 計算SHA-1碼(大小寫):Hash_SHA_1
⑥ 計算SHA-256碼(大小寫):Hash_SHA_256
⑦ 計算SHA-384碼(大小寫):Hash_SHA_384
⑧ 計算SHA-512碼(大小寫):Hash_SHA_512
編譯后被打包成文件HashTools.dll,其他程序可以在添加引用后對這些函數(shù)進(jìn)行調(diào)用
二、類庫中各函數(shù)代碼
1. 類庫結(jié)構(gòu)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashTools
{
public class HashHelper
{
//各個函數(shù)
}
}
2. 計算32位MD5碼(大小寫):Hash_MD5_32
/// <summary>
/// 計算32位MD5碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_MD5_32(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear();
//根據(jù)計算得到的Hash碼翻譯為MD5碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
3. 計算16位MD5碼(大小寫):Hash_MD5_16
/// <summary>
/// 計算16位MD5碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_MD5_16(string word, bool toUpper = true)
{
try
{
string sHash = Hash_MD5_32(word).Substring(8, 16);
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
4. 計算32位2重MD5碼(大小寫):Hash_2_MD5_32
/// <summary>
/// 計算32位2重MD5碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_2_MD5_32(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue);
//根據(jù)計算得到的Hash碼翻譯為MD5碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
bytValue = System.Text.Encoding.UTF8.GetBytes(sHash);
bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear();
sHash = "";
//根據(jù)計算得到的Hash碼翻譯為MD5碼
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
5. 計算16位2重MD5碼(大小寫):Hash_2_MD5_16
/// <summary>
/// 計算16位2重MD5碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_2_MD5_16(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue);
//根據(jù)計算得到的Hash碼翻譯為MD5碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
sHash = sHash.Substring(8, 16);
bytValue = System.Text.Encoding.UTF8.GetBytes(sHash);
bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear();
sHash = "";
//根據(jù)計算得到的Hash碼翻譯為MD5碼
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
sHash = sHash.Substring(8, 16);
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
6. 計算SHA-1碼(大小寫):Hash_SHA_1
/// <summary>
/// 計算SHA-1碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_SHA_1(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA1CryptoServiceProvider SHA1CSP
= new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA1CSP.ComputeHash(bytValue);
SHA1CSP.Clear();
//根據(jù)計算得到的Hash碼翻譯為SHA-1碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
7. 計算SHA-256碼(大小寫):Hash_SHA_256
/// <summary>
/// 計算SHA-256碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_SHA_256(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP
= new System.Security.Cryptography.SHA256CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA256CSP.ComputeHash(bytValue);
SHA256CSP.Clear();
//根據(jù)計算得到的Hash碼翻譯為SHA-1碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
8. 計算SHA-384碼(大小寫):Hash_SHA_384
/// <summary>
/// 計算SHA-384碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_SHA_384(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP
= new System.Security.Cryptography.SHA384CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA384CSP.ComputeHash(bytValue);
SHA384CSP.Clear();
//根據(jù)計算得到的Hash碼翻譯為SHA-1碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
9. 計算SHA-512碼(大小寫):Hash_SHA_512
/// <summary>
/// 計算SHA-512碼
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大寫,false:英文小寫</param>
/// <returns></returns>
public static string Hash_SHA_512(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
= new System.Security.Cryptography.SHA512CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA512CSP.ComputeHash(bytValue);
SHA512CSP.Clear();
//根據(jù)計算得到的Hash碼翻譯為SHA-1碼
string sHash = "", sTemp = "";
for (int counter = 0; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / 16;
if (i > 9)
{
sTemp = ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % 16;
if (i > 9)
{
sTemp += ((char)(i - 10 + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
}
//根據(jù)大小寫規(guī)則決定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
三、函數(shù)調(diào)用
建立項目ComputeHash,添加對HashTools.dll庫的引用。并添加代碼:
然后在Main函數(shù)中添加下列代碼:
static void Main(string[] args)
{
Console.WriteLine("MD5 of \"abc\"");
Console.WriteLine("MD5_32(Upper): {0}",
HashHelper.Hash_MD5_32("abc"));
Console.WriteLine("MD5_32(Lower): {0}",
HashHelper.Hash_MD5_32("abc", false));
Console.WriteLine("MD5_16(Upper): {0}",
HashHelper.Hash_MD5_16("abc"));
Console.WriteLine("MD5_16(Lower): {0}",
HashHelper.Hash_MD5_16("abc", false));
Console.WriteLine("2_MD5_32(Upper): {0}",
HashHelper.Hash_2_MD5_32("abc"));
Console.WriteLine("2_MD5_32(Lower): {0}",
HashHelper.Hash_2_MD5_32("abc", false));
Console.WriteLine("2_MD5_32(Upper): {0}",
HashHelper.Hash_2_MD5_16("abc"));
Console.WriteLine("2_MD5_32(Lower): {0}",
HashHelper.Hash_2_MD5_16("abc", false));
Console.WriteLine("SHA of \"abc\"");
Console.WriteLine("SHA-1(Upper): {0}",
HashHelper.Hash_SHA_1("abc"));
Console.WriteLine("SHA-1(Lower): {0}",
HashHelper.Hash_SHA_1("abc", false));
Console.WriteLine("SHA-256(Upper): {0}",
HashHelper.Hash_SHA_256("abc"));
Console.WriteLine("SHA-256(Lower): {0}",
HashHelper.Hash_SHA_256("abc", false));
Console.WriteLine("SHA-384(Upper): {0}",
HashHelper.Hash_SHA_384("abc"));
Console.WriteLine("SHA-384(Lower): {0}",
HashHelper.Hash_SHA_384("abc", false));
Console.WriteLine("SHA-512(Upper): {0}",
HashHelper.Hash_SHA_512("abc"));
Console.WriteLine("SHA-512(Lower): {0}",
HashHelper.Hash_SHA_512("abc", false));
Console.ReadLine();
}
運行結(jié)果如下:

希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C# Winform 實現(xiàn)屏蔽鍵盤的win和alt+F4的實現(xiàn)代碼
最近在做一個惡搞程序,就是打開后,程序獲得桌面的截圖然后,然后全屏顯示在屏幕上,用戶此時則不能進(jìn)行任何操作。2009-02-02
WPF使用DrawingContext實現(xiàn)二維繪圖
這篇文章介紹了WPF使用DrawingContext實現(xiàn)二維繪圖的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#動態(tài)執(zhí)行字符串(動態(tài)創(chuàng)建代碼)的實例代碼
在編寫C#程序的時候,有時我們需要動態(tài)生成一些代碼并執(zhí)行。然而C#不像JavaScript有一個Eval函數(shù),可以動態(tài)的執(zhí)行代碼。所有這些功能都要我們自己去完成2013-03-03
WPF實現(xiàn)類似ChatGPT逐字打印效果的示例代碼
前一段時間ChatGPT類的應(yīng)用十分火爆,這類應(yīng)用在回答用戶的問題時逐字打印輸出,像極了真人打字回復(fù)消息,本文就來利用WPF模擬一下這種逐字打印的效果吧2023-08-08

