哈希与校验
开发专题 · MD5 / SHA1 / SHA256 / CRC32(.NET Framework 4.8)
本页说明在 .NET Framework 4.8 项目中计算常见摘要哈希与 CRC32 校验。目标框架以 LCode 默认的 .NET 4.8 为准;示例面向 C# 5.0 与 VB.NET(勿用字符串插值、nameof 等)。操作文案以 LCode 中文界面为准。哈希用于完整性校验或指纹,不是加密;可逆加解密见 加解密。读文件再哈希见 文件与流读写。
选用哪种算法
| 算法 | 类型 / 命名空间 | 说明 |
|---|---|---|
| MD5 | System.Security.Cryptography.MD5 |
32 位十六进制常见;已不适合做安全口令存储,仅作兼容/校验指纹 |
| SHA1 | System.Security.Cryptography.SHA1 |
40 位十六进制;安全场景已偏弱,协议兼容时仍常见 |
| SHA256 | System.Security.Cryptography.SHA256 |
64 位十六进制;一般推荐的摘要算法 |
| CRC32 | 自写小函数(框架无内置 CRC32) | 4 字节校验和,常用于文件/协议完整性;非加密哈希 |
在 LCode 中使用
- 按 窗体应用入门 建好项目。
- 代码中
using System.Security.Cryptography;/Imports System.Security.Cryptography。窗体项目一般已引用含该命名空间的框架程序集,通常不必再「添加引用」。 - 菜单 编译 → 编译解决方案(F8)验证。
提示:对文件哈希时先用
File.ReadAllBytes(path) 或流式 ComputeHash(stream),路径优先 Path.Combine(Application.StartupPath, ...)。
共用:字节 → 十六进制
下列摘要示例都把 byte[] 转成无分隔小写十六进制字符串。
C#
using System;
using System.Security.Cryptography;
using System.Text;
static string ToHex(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 2);
for (int i = 0; i < data.Length; i++)
sb.Append(data[i].ToString("x2"));
return sb.ToString();
}
VB.NET
Imports System
Imports System.Security.Cryptography
Imports System.Text
Private Shared Function ToHex(data As Byte()) As String
Dim sb As New StringBuilder(data.Length * 2)
For i As Integer = 0 To data.Length - 1
sb.Append(data(i).ToString("x2"))
Next
Return sb.ToString()
End Function
MD5
C#
byte[] input = Encoding.UTF8.GetBytes("hello");
string hex;
using (MD5 md5 = MD5.Create())
{
hex = ToHex(md5.ComputeHash(input));
}
// hex = "5d41402abc4b2a76b9719d911017c592"
VB.NET
Dim input As Byte() = Encoding.UTF8.GetBytes("hello")
Dim hex As String
Using md5 As MD5 = MD5.Create()
hex = ToHex(md5.ComputeHash(input))
End Using
' hex = "5d41402abc4b2a76b9719d911017c592"
SHA1
C#
byte[] input = Encoding.UTF8.GetBytes("hello");
string hex;
using (SHA1 sha1 = SHA1.Create())
{
hex = ToHex(sha1.ComputeHash(input));
}
VB.NET
Dim input As Byte() = Encoding.UTF8.GetBytes("hello")
Dim hex As String
Using sha1 As SHA1 = SHA1.Create()
hex = ToHex(sha1.ComputeHash(input))
End Using
SHA256
C#
byte[] input = Encoding.UTF8.GetBytes("hello");
string hex;
using (SHA256 sha = SHA256.Create())
{
hex = ToHex(sha.ComputeHash(input));
}
VB.NET
Dim input As Byte() = Encoding.UTF8.GetBytes("hello")
Dim hex As String
Using sha As SHA256 = SHA256.Create()
hex = ToHex(sha.ComputeHash(input))
End Using
CRC32
.NET Framework 4.8 无内置 CRC32,下面为常用 IEEE 多项式的短实现(返回 uint / 8 位十六进制)。
C#
static uint Crc32(byte[] data)
{
uint crc = 0xFFFFFFFFu;
for (int i = 0; i < data.Length; i++)
{
crc ^= data[i];
for (int b = 0; b < 8; b++)
{
if ((crc & 1u) != 0u)
crc = (crc >> 1) ^ 0xEDB88320u;
else
crc = crc >> 1;
}
}
return ~crc;
}
byte[] input = Encoding.UTF8.GetBytes("hello");
string hex = Crc32(input).ToString("x8");
VB.NET
Private Shared Function Crc32(data As Byte()) As UInteger
Dim crc As UInteger = &HFFFFFFFFu
For i As Integer = 0 To data.Length - 1
crc = crc Xor data(i)
For b As Integer = 0 To 7
If (crc And 1UI) <> 0UI Then
crc = (crc >> 1) Xor &HEDB88320UI
Else
crc = crc >> 1
End If
Next
Next
Return Not crc
End Function
Dim input As Byte() = Encoding.UTF8.GetBytes("hello")
Dim hex As String = Crc32(input).ToString("x8")
易错点
| 点 | 说明 |
|---|---|
| 编码 | 字符串先转字节时编码须固定(常用 UTF-8);编码不同则摘要不同。 |
| 大小写 / 分隔 | 与外部系统对账时统一十六进制大小写,以及是否带 - 分隔。 |
| Dispose | MD5/SHA* 实现 IDisposable,用 using / Using 释放。 |
| 安全用途 | 口令存储勿用裸 MD5/SHA1;本页只讲摘要与校验。可逆加解密见 加解密。 |
| C# 语言版本 | LCode / .NET 4.8 按 C# 5.0:勿用 $"…"、nameof 等。 |