跳转到内容

CRC64Helper

CRC64Helper 是一个静态帮助类,提供 CRC64 校验和算法的计算能力,采用 ISO 3309 / ECMA-182 反射变体多项式(0xC96C5795D7870F42),初始值为 0、不输出反射,适用于数据完整性校验、文件指纹等非安全用途。

  • 🔢 标准算法 — ISO 3309 / ECMA-182 反射变体多项式 0xC96C5795D7870F42,初始值 0、不输出反射
  • 📝 多输入支持 — 接受字节数组、字符串(UTF-8 编码)输入
  • 🎨 多输出格式 — 支持无符号整数、小写十六进制、8 字节小端序字节数组
  • 📁 文件校验 — 分块读取文件流计算校验和(8KB 缓冲),适合大文件
  • ⚡ 查表优化 — 内置 256 项预计算查找表,单字节处理为查表 + 异或
  • 🌐 多目标框架 — 同时支持 netstandard2.1 与 net8.0
namespace Chet.Security;
public static class CRC64Helper
{
public static ulong Compute(byte[] data);
public static string ComputeHex(byte[] data);
public static string ComputeHex(string text);
public static string ComputeFileHex(string filePath);
public static byte[] ComputeBytes(byte[] data);
}

计算给定字节数组的 CRC64 校验和,返回 64 位无符号整数。

public static ulong Compute(byte[] data);
参数名类型描述
databyte[]输入字节数组

返回值: CRC64 校验和(64 位无符号整数)。

异常: data 为 null 时抛出 ArgumentNullException。

示例:

using Chet.Security;
byte[] data = "hello"u8.ToArray();
ulong crc = CRC64Helper.Compute(data);
// crc: 64 位无符号整数

计算给定字节数组的 CRC64 校验和,并以小写十六进制返回。

public static string ComputeHex(byte[] data);
参数名类型描述
databyte[]输入字节数组

返回值: 小写十六进制校验和字符串(不带前导零填充,长度依数值而定)。

示例:

using Chet.Security;
byte[] data = "hello"u8.ToArray();
string hex = CRC64Helper.ComputeHex(data);
// hex: 16 字符小写十六进制

将字符串按 UTF-8 编码后计算 CRC64 校验和,并以小写十六进制返回。

public static string ComputeHex(string text);
参数名类型描述
textstring待计算的字符串

返回值: 小写十六进制校验和字符串。

异常: text 为 null 或空字符串时抛出 ArgumentException。

示例:

using Chet.Security;
string hex = CRC64Helper.ComputeHex("hello");
// hex: 16 字符小写十六进制

计算 CRC64 校验和,并以 8 字节小端序返回。

public static byte[] ComputeBytes(byte[] data);
参数名类型描述
databyte[]输入字节数组

返回值: 8 字节长度的小端序校验和字节数组。

示例:

using Chet.Security;
byte[] bytes = CRC64Helper.ComputeBytes("hello"u8.ToArray());
// bytes.Length == 8(小端序)

分块读取文件并计算 CRC64 校验和,返回小写十六进制。

public static string ComputeFileHex(string filePath);
参数名类型描述
filePathstring文件绝对或相对路径

返回值: 小写十六进制校验和字符串。

异常: filePath 为 null 或空字符串时抛出 ArgumentException;文件不存在时抛出 FileNotFoundException。

示例:

using Chet.Security;
string hex = CRC64Helper.ComputeFileHex(@"C:\Files\archive.zip");
// hex: 16 字符小写十六进制(依文件内容而定)