C#计算字符串哈希值(MD5、SHA)的方法小结
作者:北风其凉 时间:2022-07-02 15:47:55
本文实例讲述了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#,哈希,MD5,SHA
0
投稿
猜你喜欢
Spring Boot集成MyBatis实现通用Mapper的配置及使用
2023-07-03 18:17:10
SpringBoot 如何编写配置文件
2023-07-14 10:38:24
Springboot Vue实现单点登陆功能示例详解
2023-11-05 00:29:11
SpringBoot集成支付宝沙箱支付(支付、退款)
2022-02-15 16:50:52
基于Hadoop实现Knn算法
2023-11-27 04:01:20
解决JSTL foEach标签 刷新报错的方法
2021-08-12 13:48:25
Java中MultipartFile与File互转的实现
2022-02-15 15:36:02
SpringMVC深入讲解文件的上传下载实现
2022-04-21 09:01:51
mybatis打印SQL,并显示参数的实例
2022-08-03 16:46:59
springboot 实战:异常与重定向问题
2022-03-06 15:44:54
Kotlin协程操作之创建启动挂起恢复详解
2022-07-01 09:25:25
IDEA中java断言assert语法及使用
2022-12-28 21:07:00
解读@RequestBody与post请求的关系
2022-10-07 02:02:51
详解ThreadLocal为什么会内存溢出原理
2023-11-09 18:45:26
java对象转换String类型的三种方法
2023-11-09 15:50:59
Android颜色配置器配置方法
2022-06-10 23:39:33
Spring Security如何实现升级密码加密方式详解
2023-09-02 08:47:31
Flutter 使用fluro的转场动画进行页面切换
2023-06-17 11:49:26
C#实现自定义动画鼠标的示例详解
2021-07-31 17:16:59
盘点几种常见的java排序算法
2023-09-17 10:13:10