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);
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章