C#計(jì)算字符串哈希值(MD5、SHA)的方法小結(jié)
本文實(shí)例講述了C#計(jì)算字符串哈希值(MD5、SHA)的方法。分享給大家供大家參考。具體如下:
一、關(guān)于本文
本文中是一個(gè)類(lèi)庫(kù),包括下面幾個(gè)函數(shù):
① 計(jì)算32位MD5碼(大小寫(xiě)):Hash_MD5_32
② 計(jì)算16位MD5碼(大小寫(xiě)):Hash_MD5_16
③ 計(jì)算32位2重MD5碼(大小寫(xiě)):Hash_2_MD5_32
④ 計(jì)算16位2重MD5碼(大小寫(xiě)):Hash_2_MD5_16
⑤ 計(jì)算SHA-1碼(大小寫(xiě)):Hash_SHA_1
⑥ 計(jì)算SHA-256碼(大小寫(xiě)):Hash_SHA_256
⑦ 計(jì)算SHA-384碼(大小寫(xiě)):Hash_SHA_384
⑧ 計(jì)算SHA-512碼(大小寫(xiě)):Hash_SHA_512
編譯后被打包成文件HashTools.dll,其他程序可以在添加引用后對(duì)這些函數(shù)進(jìn)行調(diào)用
二、類(lèi)庫(kù)中各函數(shù)代碼
1. 類(lèi)庫(kù)結(jié)構(gòu)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashTools { public class HashHelper { //各個(gè)函數(shù) } }
2. 計(jì)算32位MD5碼(大小寫(xiě)):Hash_MD5_32
/// <summary> /// 計(jì)算32位MD5碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
3. 計(jì)算16位MD5碼(大小寫(xiě)):Hash_MD5_16
/// <summary> /// 計(jì)算16位MD5碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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. 計(jì)算32位2重MD5碼(大小寫(xiě)):Hash_2_MD5_32
/// <summary> /// 計(jì)算32位2重MD5碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
5. 計(jì)算16位2重MD5碼(大小寫(xiě)):Hash_2_MD5_16
/// <summary> /// 計(jì)算16位2重MD5碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
6. 計(jì)算SHA-1碼(大小寫(xiě)):Hash_SHA_1
/// <summary> /// 計(jì)算SHA-1碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
7. 計(jì)算SHA-256碼(大小寫(xiě)):Hash_SHA_256
/// <summary> /// 計(jì)算SHA-256碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
8. 計(jì)算SHA-384碼(大小寫(xiě)):Hash_SHA_384
/// <summary> /// 計(jì)算SHA-384碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
9. 計(jì)算SHA-512碼(大小寫(xiě)):Hash_SHA_512
/// <summary> /// 計(jì)算SHA-512碼 /// </summary> /// <param name="word">字符串</param> /// <param name="toUpper">返回哈希值格式 true:英文大寫(xiě),false:英文小寫(xiě)</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ù)計(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ù)大小寫(xiě)規(guī)則決定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
三、函數(shù)調(diào)用
建立項(xiàng)目ComputeHash,添加對(duì)HashTools.dll庫(kù)的引用。并添加代碼:
然后在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(); }
運(yùn)行結(jié)果如下:
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實(shí)例
- C#實(shí)現(xiàn)給定字符串生成MD5哈希的方法
- c#多種加解密示例(md5加密解密)
- c# 實(shí)現(xiàn)MD5,SHA1,SHA256,SHA512等常用加密算法源代碼
- C#生成MD5的函數(shù)代碼
- C#計(jì)算文件MD5校驗(yàn)的方法
- 淺談C#中Md5和Sha1兩種加密方式
- C#實(shí)現(xiàn)的MD5加密功能與用法示例
- C#獲取文件MD5值的實(shí)現(xiàn)示例
- C#實(shí)現(xiàn)將32位MD5摘要串轉(zhuǎn)換為128位二進(jìn)制字符串的方法
相關(guān)文章
C#使用泛型方法實(shí)現(xiàn)操作不同數(shù)據(jù)類(lèi)型的數(shù)組
這篇文章主要為大家詳細(xì)介紹了C#如何使用一個(gè)泛型方法對(duì)不同類(lèi)型的數(shù)組進(jìn)行操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2024-02-02C#獲取鼠標(biāo)在listview右鍵點(diǎn)擊單元格的內(nèi)容方法
下面小編就為大家?guī)?lái)一篇C#獲取鼠標(biāo)在listview右鍵點(diǎn)擊單元格的內(nèi)容方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01C# VB 實(shí)現(xiàn)10進(jìn)制 16進(jìn)制之間互相轉(zhuǎn)換
如何將10進(jìn)制轉(zhuǎn)成16進(jìn)制,又如何將16進(jìn)制數(shù)轉(zhuǎn)成10進(jìn)制,本文將介紹C#和VB實(shí)現(xiàn)代碼,需要了解的朋友可以參考下2012-11-11C#匿名方法與Delegate類(lèi)型轉(zhuǎn)換錯(cuò)誤分析
這篇文章主要介紹了C#匿名方法與Delegate類(lèi)型轉(zhuǎn)換錯(cuò)誤,較為詳細(xì)的分析了C#匿名方法的用法及Delegate類(lèi)型轉(zhuǎn)換錯(cuò)誤問(wèn)題解決方法,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11c#中directory 和directoryinfo的使用小結(jié)
當(dāng)使用C#處理目錄時(shí),可以使用?System.IO?命名空間中的?Directory?和?DirectoryInfo?類(lèi)來(lái)執(zhí)行各種目錄操作,本文主要介紹了c#中directory 和directoryinfo的使用小結(jié),感興趣的可以了解一下2024-02-02C#使用ZBar實(shí)現(xiàn)識(shí)別條形碼
目前主流的識(shí)別庫(kù)主要有ZXing.NET和ZBar,本文主要介紹的是如何使用ZBar庫(kù)實(shí)現(xiàn)識(shí)別條形碼功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07