// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Spludlow { public class CRC32 { private readonly uint[] ChecksumTable; private readonly uint Polynomial = 0xEDB88320; public CRC32() { ChecksumTable = new uint[0x100]; for (uint index = 0; index < 0x100; ++index) { uint item = index; for (int bit = 0; bit < 8; ++bit) item = ((item & 1) != 0) ? (Polynomial ^ (item >> 1)) : (item >> 1); ChecksumTable[index] = item; } } public byte[] ComputeHash(Stream stream) { uint result = 0xFFFFFFFF; int current; while ((current = stream.ReadByte()) != -1) result = ChecksumTable[(result & 0xFF) ^ (byte)current] ^ (result >> 8); byte[] hash = BitConverter.GetBytes(~result); Array.Reverse(hash); return hash; } public byte[] ComputeHash(byte[] data) { using (MemoryStream stream = new MemoryStream(data)) return ComputeHash(stream); } } }