分类
标签
.NET 9008 adb android apt asp.net ASP.NET Core audio bug C c++ C++ chrome cmd csharp CSharp css css3 debian debug dotnet dotnet Framework dpkg GDI&GDI+ gdi&gdi+ golang graphics html html5 http java javascript json kali linux linux mci microsoft minimap MSBuild mysql OpenCV PInvoke powershell python QQ rust shell speex sql tutorial ubuntu ui unity vb.net visual studio Visual Studio web Web win32 winapi windows winform WinForm wpf WPF xaml xfce 列表 刷机 前端 加密 反射 反编译 可视化 图像处理 多线程 字符串 安卓 实例 局域网 幻影坦克 库 开发语言 异步 微信 手册 手机 接口 摘要 救砖 数字签名 数字证书 数字音频 数据库 桌面程序 游戏 游戏引擎 源码 爬虫 玩游戏 电脑硬件 笔记 算法 类库 线性代数 编程语言 网络 脚本语言 计算机图形学 计算机基础 设计模式 语音编解码 运维 进制 面向对象编程 音频 音频编码解码
609 字
3 分钟
[C#] 计算 MD5 SHA1 SHA256 SHA384 SHA512 CRC32
直接贴代码了, 复制即可用, 源码部分来自网络.
如果要计算字符串的 MD5 值, 直接 Encoding.UTF8.GetBytes() 然后就可以了
using System;
using System.IO;
using System.Linq;
namespace NullLib.HashCalc
{
public class HashHelper
{
public static string CalcMd5x32(byte[] bytValue)
{
return CalcMd5x32(new MemoryStream(bytValue));
}
public static string CalcMd5x16(byte[] bytValue)
{
return CalcMd5x16(new MemoryStream(bytValue));
}
public static string CalcShax1(byte[] bytValue)
{
return CalcShax1(new MemoryStream(bytValue));
}
public static string CalcShax256(byte[] bytValue)
{
return CalcShax256(new MemoryStream(bytValue));
}
public static string CalcShax384(byte[] bytValue)
{
return CalcShax384(new MemoryStream(bytValue));
}
public static string CalcShax512(byte[] bytValue)
{
return CalcShax512(new MemoryStream(bytValue));
}
public static string CalcMd5x32(Stream stream)
{
var MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytHash = MD5CSP.ComputeHash(stream);
MD5CSP.Clear();
string sHash = BytesToHex(bytHash);
return sHash;
}
public static string CalcMd5x16(Stream stream)
{
return CalcMd5x32(stream).Substring(8, 16);
}
public static string CalcShax1(Stream stream)
{
var SHA1CSP = new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] bytHash = SHA1CSP.ComputeHash(stream);
SHA1CSP.Clear();
string sHash = BytesToHex(bytHash);
return sHash;
}
public static string CalcShax256(Stream stream)
{
var SHA256CSP = new System.Security.Cryptography.SHA256CryptoServiceProvider();
byte[] bytHash = SHA256CSP.ComputeHash(stream);
SHA256CSP.Clear();
string sHash = BytesToHex(bytHash);
return sHash;
}
public static string CalcShax384(Stream stream)
{
var SHA384CSP = new System.Security.Cryptography.SHA384CryptoServiceProvider();
byte[] bytHash = SHA384CSP.ComputeHash(stream);
SHA384CSP.Clear();
string sHash = BytesToHex(bytHash);
return sHash;
}
public static string CalcShax512(Stream stream)
{
var SHA512CSP = new System.Security.Cryptography.SHA512CryptoServiceProvider();
byte[] bytHash = SHA512CSP.ComputeHash(stream);
SHA512CSP.Clear();
string sHash = BytesToHex(bytHash);
return sHash;
}
public static string CalcCrcx32(Stream stream)
{
Crc32 calculator = new Crc32();
byte[] buffer = calculator.ComputeHash(stream);
calculator.Clear();
//将字节数组转换成十六进制的字符串形式
string sHash = BytesToHex(buffer);
return sHash;
}
private static string BytesToHex(byte[] array)
{
return string.Join(null, array.Select(v => Convert.ToString(v, 16).PadLeft(2, '0')));
}
/// <summary>
/// 提供 CRC32 算法的实现
/// </summary>
private class Crc32 : System.Security.Cryptography.HashAlgorithm
{
public const uint DefaultPolynomial = 0xedb88320;
public const uint DefaultSeed = 0xffffffff;
private uint hash;
private uint seed;
private uint[] table;
private static uint[] defaultTable;
public Crc32()
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
Initialize();
}
public Crc32(uint polynomial, uint seed)
{
table = InitializeTable(polynomial);
this.seed = seed;
Initialize();
}
public override void Initialize()
{
hash = seed;
}
protected override void HashCore(byte[] buffer, int start, int length)
{
hash = CalculateHash(table, hash, buffer, start, length);
}
protected override byte[] HashFinal()
{
byte[] hashBuffer = uintToBigEndianBytes(~hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
public static uint Compute(byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
}
public static uint Compute(uint seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
}
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
private static uint[] InitializeTable(uint polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
{
return defaultTable;
}
uint[] createTable = new uint[256];
for (int i = 0; i < 256; i++)
{
uint entry = (uint)i;
for (int j = 0; j < 8; j++)
{
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
}
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
{
defaultTable = createTable;
}
return createTable;
}
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
{
uint crc = seed;
for (int i = start; i < size; i++)
{
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
}
return crc;
}
private byte[] uintToBigEndianBytes(uint x)
{
return new byte[] { (byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) };
}
}
}
}
[C#] 计算 MD5 SHA1 SHA256 SHA384 SHA512 CRC32
https://slimenull.com/posts/20210418023855/