public class Crc
{
private static ushort[] Crc16Table = null;
private static uint[] Crc32Table = null;
private static void MakeCRC16Table()
{
if (Crc16Table != null) return;
Crc16Table = new ushort[256];
for (ushort i = 0; i < 256; i++)
{
ushort vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (ushort)(vCRC >> 1);
else vCRC = (ushort)((vCRC >> 1) ^ 0x8408);
Crc16Table[i] = vCRC;
}
}
private static void MakeCRC32Table()
{
if (Crc32Table != null) return;
Crc32Table = new uint[256];
for (uint i = 0; i < 256; i++)
{
uint vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (uint)(vCRC >> 1);
else vCRC = (uint)((vCRC >> 1) ^ 0xEDB88320);
Crc32Table[i] = vCRC;
}
}
public static ushort UpdateCRC16(byte bt, ushort seed)
{
return (ushort)(Crc16Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static uint UpdateCRC32(byte bt, uint seed)
{
return (uint)(Crc32Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static ushort CRC16(byte[] bts)
{
MakeCRC16Table();
ushort Result = 0xFFFF;
foreach (byte vByte in bts)
Result = UpdateCRC16(vByte, Result);
return (ushort)(~Result);
}
public static ushort CRC16(string str, Encoding encoding)
{
return CRC16(encoding.GetBytes(str));
}
public static ushort CRC16(string str)
{
return CRC16(str, Encoding.UTF8);
}
public static uint CRC32(byte[] bts)
{
MakeCRC32Table();
uint result = 0xFFFFFFFF;
foreach (byte vByte in bts)
result = UpdateCRC32(vByte, result);
return (uint)(~result);
}
public static uint CRC32(string str, Encoding encoding)
{
return CRC32(encoding.GetBytes(str));
}
public static uint CRC32(string str)
{
return CRC32(str, Encoding.Unicode);
}
}
{
private static ushort[] Crc16Table = null;
private static uint[] Crc32Table = null;
private static void MakeCRC16Table()
{
if (Crc16Table != null) return;
Crc16Table = new ushort[256];
for (ushort i = 0; i < 256; i++)
{
ushort vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (ushort)(vCRC >> 1);
else vCRC = (ushort)((vCRC >> 1) ^ 0x8408);
Crc16Table[i] = vCRC;
}
}
private static void MakeCRC32Table()
{
if (Crc32Table != null) return;
Crc32Table = new uint[256];
for (uint i = 0; i < 256; i++)
{
uint vCRC = i;
for (int j = 0; j < 8; j++)
if (vCRC % 2 == 0)
vCRC = (uint)(vCRC >> 1);
else vCRC = (uint)((vCRC >> 1) ^ 0xEDB88320);
Crc32Table[i] = vCRC;
}
}
public static ushort UpdateCRC16(byte bt, ushort seed)
{
return (ushort)(Crc16Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static uint UpdateCRC32(byte bt, uint seed)
{
return (uint)(Crc32Table[(seed & 0x000000FF) ^ bt] ^ (seed >> 8));
}
public static ushort CRC16(byte[] bts)
{
MakeCRC16Table();
ushort Result = 0xFFFF;
foreach (byte vByte in bts)
Result = UpdateCRC16(vByte, Result);
return (ushort)(~Result);
}
public static ushort CRC16(string str, Encoding encoding)
{
return CRC16(encoding.GetBytes(str));
}
public static ushort CRC16(string str)
{
return CRC16(str, Encoding.UTF8);
}
public static uint CRC32(byte[] bts)
{
MakeCRC32Table();
uint result = 0xFFFFFFFF;
foreach (byte vByte in bts)
result = UpdateCRC32(vByte, result);
return (uint)(~result);
}
public static uint CRC32(string str, Encoding encoding)
{
return CRC32(encoding.GetBytes(str));
}
public static uint CRC32(string str)
{
return CRC32(str, Encoding.Unicode);
}
}