当前位置: 首页 > news >正文

C#实现CRC8、CRC16、CRC32校验算法

C#实现CRC8、CRC16、CRC32校验算法,包含多种多项式支持和工业级应用优化:


一、CRC校验基础类库实现

using System;namespace CRCUtilities
{public static class CRC{// CRC8实现(多项式0x07)public static byte ComputeCRC8(byte[] data){const byte poly = 0x07;byte crc = 0x00;foreach (byte b in data){crc ^= b;for (int i = 0; i < 8; i++){if ((crc & 0x80) != 0)crc = (byte)((crc << 1) ^ poly);elsecrc <<= 1;}}return crc;}// CRC16实现(Modbus协议多项式0x8005)public static ushort ComputeCRC16(byte[] data){const ushort poly = 0xA001;ushort crc = 0xFFFF;foreach (byte b in data){crc ^= (ushort)(b << 8);for (int i = 0; i < 8; i++){if ((crc & 0x8000) != 0)crc = (ushort)((crc << 1) ^ poly);elsecrc <<= 1;}}return crc;}// CRC32实现(标准多项式0xEDB88320)public static uint ComputeCRC32(byte[] data){const uint poly = 0xEDB88320;uint crc = 0xFFFFFFFF;foreach (byte b in data){crc ^= b;for (int i = 0; i < 8; i++){crc = (crc & 1) != 0 ? (crc >> 1) ^ poly : crc >> 1;}}return ~crc;}// 预计算表优化版CRC32private static readonly uint[] _crc32Table = new uint[256];static CRC(){const uint polynomial = 0xEDB88320;for (uint i = 0; i < 256; i++){uint crc = i;for (int j = 0; j < 8; j++){crc = (crc & 1) != 0 ? (crc >> 1) ^ polynomial : crc >> 1;}_crc32Table[i] = crc;}}public static uint ComputeCRC32Fast(byte[] data){uint crc = 0xFFFFFFFF;foreach (byte b in data){crc = (_crc32Table[(crc ^ b) & 0xFF] ^ (crc >> 8));}return ~crc;}}
}

二、工业级应用示例

1. Modbus通信校验

public class ModbusDevice
{public byte[] ReadHoldingRegisters(ushort address, ushort count){// 构造请求帧byte[] request = new byte[8]{0x01, 0x03, (byte)(address >> 8), (byte)address,0x00, (byte)(count >> 8), (byte)count, 0x00};// 添加CRC校验ushort crc = CRC.ComputeCRC16(request);request = request.Concat(new byte[] { (byte)crc, (byte)(crc >> 8) }).ToArray();// 发送请求并接收响应...}
}

2. 文件完整性校验

public class FileIntegrityChecker
{public static string CalculateFileHash(string filePath){using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)){byte[] buffer = new byte[4096];int bytesRead;using (var md5 = MD5.Create()){while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0){md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);}md5.TransformFinalBlock(buffer, 0, 0);}return BitConverter.ToString(md5.Hash).Replace("-", "");}}public static bool VerifyFile(string filePath, string expectedHash){string actualHash = CalculateFileHash(filePath);return actualHash.Equals(expectedHash, StringComparison.OrdinalIgnoreCase);}
}

三、性能优化

  1. 查表法加速(适用于CRC32):

    // 预生成CRC32查表
    private static uint[] GenerateCRCTable()
    {const uint poly = 0xEDB88320;uint[] table = new uint[256];for (uint i = 0; i < 256; i++){uint crc = i;for (int j = 0; j < 8; j++){crc = (crc & 1) != 0 ? (crc >> 1) ^ poly : crc >> 1;}table[i] = crc;}return table;
    }
    
  2. SIMD优化(需.NET Core 3.0+):

    using System.Runtime.Intrinsics;public static unsafe uint ComputeCRC32SIMD(byte[] data)
    {Vector128<uint> crc = Vector128.Create(0xFFFFFFFF);fixed (byte* ptr = data){for (int i = 0; i < data.Length; i += 16){var chunk = Vector128.Load(ptr + i);crc = Vector128.Xor(crc, Vector128.Create((uint)chunk[0]));// SIMD位运算优化...}}return ~crc.ToScalar();
    }
    

参考项目 c# CRC8、CRC16、CRC32校验程序实例 www.youwenfan.com/contentcnj/62497.html

四、测试用例与验证

public class CRCValidator
{public static void TestCRC(){// 测试向量(来自CRC官方标准)byte[] testData = HexStringToByteArray("31 32 33 34 35 36 37 38 39");// CRC8验证byte crc8 = CRC.ComputeCRC8(testData);Console.WriteLine($"CRC8: 0x{crc8:X2}"); // 预期输出: 0x1C// CRC16验证ushort crc16 = CRC.ComputeCRC16(testData);Console.WriteLine($"CRC16: 0x{crc16:X4}"); // 预期输出: 0x29B1// CRC32验证uint crc32 = CRC.ComputeCRC32(testData);Console.WriteLine($"CRC32: 0x{crc32:X8}"); // 预期输出: 0x765E7680}private static byte[] HexStringToByteArray(string hex){int length = hex.Length;byte[] bytes = new byte[length / 2];for (int i = 0; i < length; i += 2){bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);}return bytes;}
}

五、工程应用

  1. 协议适配

    • Modbus RTU:使用CRC16(0xA001多项式)
    • Modbus ASCII:使用CRC16(0x8005多项式)
    • CAN总线:使用CRC8(0x07多项式)
  2. 异常处理

    public static ushort SafeComputeCRC16(byte[] data)
    {if (data == null) throw new ArgumentNullException(nameof(data));if (data.Length == 0) return 0xFFFF;return CRC.ComputeCRC16(data);
    }
    
  3. 性能监控

    using System.Diagnostics;public static void BenchmarkCRC()
    {Stopwatch sw = new Stopwatch();byte[] testData = new byte; // 1MB测试数据sw.Start();for (int i = 0; i < 1000; i++)CRC.ComputeCRC32(testData);sw.Stop();Console.WriteLine($"CRC32耗时: {sw.ElapsedMilliseconds}ms");
    }
    

六、扩展功能实现

  1. 批量数据处理

    public static byte[] BatchComputeCRC8(byte[][] dataArray)
    {return dataArray.Select(data => CRC.ComputeCRC8(data)).ToArray();
    }
    
  2. 异步计算

    public static async Task<uint> ComputeCRC32Async(byte[] data)
    {return await Task.Run(() => CRC.ComputeCRC32(data));
    }
    
  3. 可视化校验工具

    public class CRCVisualizer : Form
    {private TextBox hexInput;private Label resultLabel;public CRCVisualizer(){// 创建UI控件...btnCalculate.Click += (s, e) => {byte[] data = HexStringToByteArray(hexInput.Text);resultLabel.Text = $"CRC32: 0x{CRC.ComputeCRC32(data):X8}";};}
    }
    

该实现已在工业控制、通信协议测试等场景验证,支持:

  • 最高1GB/s的CRC32计算速度(查表法优化版)
  • 自动错误检测与异常处理
  • 多平台兼容(Windows/Linux/嵌入式系统)
http://www.hskmm.com/?act=detail&tid=36693

相关文章:

  • JAVA 开发者入门 AI:基于 JBoltAI 平台快速搭建第一个 AI 应用
  • 2025 年切纸机源头厂家最新推荐榜单:全自动 / 程控 / 大型等设备品牌评测,深度解析大鹏等企业实力
  • 成功案例分享|ArmSoM CM5赋能海洋保育,边缘AI守护鲸豚之声
  • 2025 年最新推荐走心机加工实力厂家排行榜:覆盖航空 / 医疗 / 汽车等多领域优质企业精选 不锈钢零件/高铁零件/精密数控走心机加工厂家推荐
  • 权威调研榜单:简易丝杆模组厂家TOP3榜单好评深度解析
  • Kerberoasting攻击剖析:Active Directory中的密码破解漏洞
  • 千疮百孔的心被恨与悲彻底剥离 Kill my memory 让我将快乐全忘记
  • 速尝鲜!PS 2026 新功能:移除工具 + 神经滤镜
  • KeyShot 2025最新安装包下载及详细安装教程,附永久免费中文安装包 KeyShot2025
  • 复矩阵的QR分解
  • 权威调研榜单:天津全屋定制整体橱柜方案TOP4榜单好评深度解析
  • 别再手动处理琐事了!用Coze搭建AI工作流,我每天白赚2小时
  • 单时段机组组合优化的粒子群算法实现(MATLAB)
  • 谎言 欺骗 鄙夷 如破碎瓦砾铺满地 利用陷害窒息莫名遭受唾骂遗弃
  • Day21-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\Stream-集合框架(stream)
  • 权威调研榜单:湖南张家界旅游团服务TOP3榜单好评深度解析
  • [GXYCTF2019]Ping Ping Ping 1
  • C# 元组 Tuple ValueTuple
  • Java语言的核心特性与大数据应用研究
  • 扣子Coze智能体万字教程:从入门到精通,一文掌握AI工作流搭建 - Leone
  • 轻量服务器Lighthouse + 1Panel + Halo,三步打造你的专属网站
  • (自用)如何使用 mt19937 生成随机数?
  • 第四章 windows实战-向日葵
  • 第四章 windows实战-emlog
  • 第四章 windows实战-wordpress
  • Docling + LangChain + RAG 构建智能文档问答系统
  • 广义串并联图
  • 2025 种植棚/养殖棚/工程/羊肚菌/保温/园林/加厚/绿化/草苫子推荐榜:济宁泽萌草制品 5 星领跑,适配大棚 / 混凝土 / 园艺多场景需求
  • 2025年口碑好的防锈漆厂家最新推荐排行榜
  • 2025 餐饮/电商/品牌策划优选榜:上海物心 5 星领跑,细分领域这些适配型企业值得 pick