Класс для подсчёта CRC32 (контрольной суммы):
| Код | using System; using System.IO; using System.Text; static class CRC32 { private static readonly uint[] crc_table = new uint[256]; public const int BufferSize = 4096; static CRC32() { for (int n = 0; n < 256; ++n) { uint c = (uint) n; for (int k = 0; k < 8; ++k) if ((c & 1) != 0) c = 0xEDB88320 ^ (c >> 1); else c >>= 1; crc_table[n] = c; } } public static uint Update(uint crc, byte[] data, int offset, int count) { for (int n = offset; n < offset + count; ++n) crc = crc_table[(crc ^ data[n]) & 0xFF] ^ (crc >> 8); return crc; } public static uint Compute(byte[] data) { return Update(0xFFFFFFFF, data, 0, data.Length) ^ 0xFFFFFFFF; } public static uint Compute(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); byte[] buffer = new byte[BufferSize]; int readBytes = 0; uint crc = 0xFFFFFFFF; while ((readBytes = stream.Read(buffer, 0, BufferSize)) > 0) { crc = Update(crc, buffer, 0, readBytes); } return crc ^ 0xFFFFFFFF; } } |
Спасибо http://vingrad.ru/@Void за материал. |