C#计算字符串哈希值(MD5、SHA)的方法小结

 更新时间:2015年08月18日 15:18:34   作者:北风其凉  
这篇文章主要介绍了C#计算字符串哈希值(MD5、SHA)的方法,以实例形式较为详细的分析总结了C#计算字符串哈希值的各种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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", 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#程序设计有所帮助。

相关文章

  • C# Winform 实现屏蔽键盘的win和alt+F4的实现代码

    C# Winform 实现屏蔽键盘的win和alt+F4的实现代码

    最近在做一个恶搞程序,就是打开后,程序获得桌面的截图然后,然后全屏显示在屏幕上,用户此时则不能进行任何操作。
    2009-02-02
  • WPF使用DrawingContext实现二维绘图

    WPF使用DrawingContext实现二维绘图

    这篇文章介绍了WPF使用DrawingContext实现二维绘图的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • c#哈希算法的实现方法及思路

    c#哈希算法的实现方法及思路

    这篇文章主要介绍了c#哈希算法的实现方法及思路,有需要的朋友可以参考一下
    2013-12-12
  • C#中this指针的用法示例

    C#中this指针的用法示例

    这篇文章主要介绍了C#中this指针的用法,对初学者而言是非常重要的概念,必须加以熟练掌握,需要的朋友可以参考下
    2014-08-08
  • 详解C#App.config和Web.config加密

    详解C#App.config和Web.config加密

    本篇文章给大家分享了C#App.config和Web.config加密的相关知识点以及具体代码步骤,有兴趣的朋友参考学习下。
    2018-05-05
  • C#动态执行字符串(动态创建代码)的实例代码

    C#动态执行字符串(动态创建代码)的实例代码

    在编写C#程序的时候,有时我们需要动态生成一些代码并执行。然而C#不像JavaScript有一个Eval函数,可以动态的执行代码。所有这些功能都要我们自己去完成
    2013-03-03
  • WPF实现类似ChatGPT逐字打印效果的示例代码

    WPF实现类似ChatGPT逐字打印效果的示例代码

    前一段时间ChatGPT类的应用十分火爆,这类应用在回答用户的问题时逐字打印输出,像极了真人打字回复消息,本文就来利用WPF模拟一下这种逐字打印的效果吧
    2023-08-08
  • C#分屏控件用法实例

    C#分屏控件用法实例

    这篇文章主要介绍了C#分屏控件用法实例,需要的朋友可以参考下
    2014-08-08
  • C#中IntPtr类型的具体使用

    C#中IntPtr类型的具体使用

    本文主要介绍了C#中IntPtr类型的具体使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C# 邮箱mail 发送类

    C# 邮箱mail 发送类

    此类的功能包括发送邮件,邮箱格式是否正确,和在不发送邮件的情况下判断邮箱用户名和密码是否正确,鉴于POP检查邮箱用户名和密码出现错误情况返回结果的延迟,用异步线程解决此问题,见代码
    2015-06-06

最新评论