C#盤算字符串哈希值(MD5、SHA)的辦法小結。本站提示廣大學習愛好者:(C#盤算字符串哈希值(MD5、SHA)的辦法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C#盤算字符串哈希值(MD5、SHA)的辦法小結正文
本文實例講述了C#盤算字符串哈希值(MD5、SHA)的辦法。分享給年夜家供年夜家參考。詳細以下:
1、關於本文
本文中是一個類庫,包含上面幾個函數:
① 盤算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,其他法式可以在添加援用後對這些函數停止挪用
2、類庫中各函數代碼
1. 類庫構造
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashTools { public class HashHelper { //各個函數 } }
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(); //依據盤算獲得的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; } //依據年夜小寫規矩決議前往的字符串 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); //依據盤算獲得的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 = ""; //依據盤算獲得的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; } //依據年夜小寫規矩決議前往的字符串 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); //依據盤算獲得的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 = ""; //依據盤算獲得的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); //依據年夜小寫規矩決議前往的字符串 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(); //依據盤算獲得的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; } //依據年夜小寫規矩決議前往的字符串 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(); //依據盤算獲得的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; } //依據年夜小寫規矩決議前往的字符串 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(); //依據盤算獲得的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; } //依據年夜小寫規矩決議前往的字符串 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(); //依據盤算獲得的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; } //依據年夜小寫規矩決議前往的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); } }
3、函數挪用
樹立項目ComputeHash,添加對HashTools.dll庫的援用。並添加代碼:
using HashTools;
然後在Main函數中添加以下代碼:
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(); }
運轉成果以下:
願望本文所述對年夜家的C#法式設計有所贊助。