CRC beregning er noget "bit griseri".
:-)
Min version:
using System;
using System.Linq;
using System.Text;
using java.util.zip;
using ICSharpCode.SharpZipLib.Checksums;
using HashLib;
namespace E
{
public class Program
{
public static void TestVJSLIB(string s)
{
sbyte[] b = Encoding.UTF8.GetBytes(s).Select(v => (sbyte)v).ToArray();
CRC32 crc = new CRC32();
crc.update(b);
long crcval = crc.getValue();
Console.WriteLine("{0:X}", crcval);
}
public static void TestSharpZipLib(string s)
{
byte[] b = Encoding.UTF8.GetBytes(s);
Crc32 crc = new Crc32();
crc.Update(b);
long crcval = crc.Value;
Console.WriteLine("{0:X}", crcval);
}
public static void TestHashLib(string s)
{
byte[] b = Encoding.UTF8.GetBytes(s);
IHash crc = HashFactory.Checksum.CreateCRC32a();
uint crcval = crc.ComputeBytes(b).GetUInt();
Console.WriteLine("{0:X}", crcval);
}
public class MyCRC
{
private uint init;
private int step;
private int limit;
private int ix;
private uint[] table;
private uint mask;
public MyCRC(uint poly, uint init, int step, int limit, int ix)
{
this.init = init;
this.step = step;
this.limit = limit;
this.ix = ix;
table = new uint[16];
mask = 0xFFU >> (8 - step);
for(int i = 0; i < 16; i++)
{
uint tmp = (uint)i;
for(int j = 0; j < 4; j++)
{
uint x = tmp & 1;
tmp = tmp >> 1;
if(x == 1) tmp = tmp ^ poly;
}
table[i] = tmp;
}
}
public virtual uint Compute(byte[] b)
{
uint res = init;
for(int j = 0; j < b.Length; j++)
{
res = res ^ b[j];
for(int i = 0; i < limit; i++)
{
res = (uint)((res >> step) ^ table[(res & mask) * ix]);
}
}
return res;
}
}
public class MyCRC32 : MyCRC
{
public MyCRC32() : base(0xEDB88320, 0xFFFFFFFF, 4, 2, 1)
{
}
public override uint Compute(byte[] b)
{
return base.Compute(b) ^ 0xFFFFFFFF;
}
}
public static void TestDIY(string s)
{
byte[] b = Encoding.UTF8.GetBytes(s);
MyCRC32 crc = new MyCRC32();
uint crcval = crc.Compute(b);
Console.WriteLine("{0:X}", crcval);
}
public static void Main(string[] args)
{
string s = "This is a small test";
TestVJSLIB(s);
TestSharpZipLib(s);
TestHashLib(s);
TestDIY(s);
Console.ReadKey(true);
}
}
}