// Copyright(c) 2014 Mads Breusch Klinkby. All rights reserved. // Licensed under the MIT License; you may not use this file except in compliance with the License. // You may obtain a copy of the License at https://opensource.org/licenses/MIT // Published at https://github.com/klinkby/klinkby.checksum using System.Text; namespace Toolbox.Library.Security.Cryptography { /// /// Computes a CRC32 checksum. /// /// Based on public static class Crc32 { readonly static uint[] Table = CreateTable(); static Crc32() { } /// /// Compute the checksum of a UTF8 text. /// /// Text to calculate /// Checksum public static uint Compute(string text) { return Compute(text, Encoding.UTF8); } /// /// Compute the checksum of a text using a specific encoding. /// /// Text to calculate /// Text encoding /// Checksum public static uint Compute(string text, Encoding encoding) { if (string.IsNullOrEmpty(text)) return 0; byte[] bytes = encoding.GetBytes(text); return Compute(bytes); } /// /// Compute the checksum of a binary buffer. /// /// Buffer to calculate /// public static uint Compute(byte[] bytes) { uint crc = 0xffffffff; for (int i = 0; i < bytes.Length; ++i) { byte index = (byte)(((crc) & 0xff) ^ bytes[i]); crc = (crc >> 8) ^ Table[index]; } return unchecked((~crc)); } static uint[] CreateTable() { const uint poly = 0xedb88320; var table = new uint[256]; uint temp = 0; for (uint i = 0; i < table.Length; ++i) { temp = i; for (int j = 8; j > 0; --j) { if ((temp & 1) == 1) { temp = (uint)((temp >> 1) ^ poly); } else { temp >>= 1; } } table[i] = temp; } return table; } } }