Crc32校验

   public class Crc32
    {
        public static uint ComputeChecksum(byte[] bytes)
        {
            uint[] table = new uint[256];

            // Initialize CRC table.
            for (uint i = 0; i < 256; i++)
            {
                uint crc = i;
                for (uint j = 0; j < 8; j++)
                    crc = (crc & 1) != 0 ? (crc >> 1) ^ 0xEDB88320 : crc >> 1;
                table[i] = crc;
            }

            uint crcValue = 0xffffffff;
            foreach (byte b in bytes)
            {
                byte index = (byte)(((crcValue) & 0xff) ^ b);
                crcValue = (crcValue >> 8) ^ table[index];
            }
            return ~crcValue; // Finalize the CRC value by inverting all the bits.
        }

        public static uint ComputeChecksumFromFile(string path)
        {
            byte[] bytes = File.ReadAllBytes(path);
            return ComputeChecksum(bytes);
        }
    }

 

posted @ 2024-03-07 18:25  极客船长  阅读(3)  评论(0编辑  收藏  举报