本文实例讲述了C#计算字符串哈希值(MD5、SHA)的方法。分享给大家供大家参考。具体如下:
一、关于本文
本文中是一个类库,包括下面几个函数:
① 计算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,其他程序可以在添加引用后对这些函数进行调用
二、类库中各函数代码
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); }}
三、函数调用
建立项目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",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#程序设计有所帮助。
总结以上是内存溢出为你收集整理的C#计算字符串哈希值(MD5、SHA)的方法小结全部内容,希望文章能够帮你解决C#计算字符串哈希值(MD5、SHA)的方法小结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)