Hi
I would like to generate CRC 32 hash value by reading a file 2 MB and should read 512 bytes and calculate hash for 512 bytes and pass that generated hash value to next iteration and calculate hash of next iteration byte array along with previous hash and so on until complete 2 MB file (that is up-to 4096 iterations).
By the following code able to generate only one iteration hash not able to generate for multiple iteration by passing existing hash value.
Following is my code.
static void Main(string[] args)
{
var path = @"C:\Users\Desktop\RandomKeys.bin";
uint crc = 0;
var filedata = File.ReadAllBytes(path);
var readBuff = new uint[128];
Crc32Single crc32 = new Crc32Single();
for (int i = 0; i < 4096; i++)
{
Buffer.BlockCopy(filedata, (i * 512), readBuff, 0, 512);
byte[] aa = crc32.ComputeHash(readBuff.SelectMany(BitConverter.GetBytes).ToArray(), 0, 512);
Array.Reverse(aa);
crc = BitConverter.ToUInt32(aa, 0);
Console.WriteLine("\n********** : " + crc.ToString("X"));
}
Console.ReadLine();
}
public sealed class Crc32 : HashAlgorithm
{
public const UInt32 DefaultPolynomial = 0xedb88320u;
public const UInt32 DefaultSeed = 0xffffffffu;
static UInt32[] defaultTable;
public UInt32 seed;
readonly UInt32[] table;
UInt32 hash;
public Crc32()
: this(DefaultPolynomial, DefaultSeed)
{
}
public Crc32(UInt32 polynomial, UInt32 seed)
{
table = InitializeTable(polynomial);
this.seed = hash = seed;
}
public override void Initialize()
{
hash = seed;
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
hash = CalculateHash(table, hash, array, ibStart, cbSize);
}
protected override byte[] HashFinal()
{
var hashBuffer = UInt32ToBigEndianBytes(~hash);
HashValue = hashBuffer;
return hashBuffer;
}
public override int HashSize { get { return 32; } }
public static UInt32 Compute(byte[] buffer)
{
return Compute(DefaultSeed, buffer);
}
public static UInt32 Compute(UInt32 seed, byte[] buffer)
{
return Compute(DefaultPolynomial, seed, buffer);
}
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
static UInt32[] InitializeTable(UInt32 polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
return defaultTable;
var createTable = new UInt32[256];
for (var i = 0; i < 256; i++)
{
var entry = (UInt32)i;
for (var j = 0; j < 8; j++)
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
defaultTable = createTable;
return createTable;
}
static UInt32 CalculateHash(UInt32[] table, UInt32 seed, IList<byte> buffer, int start, int size)
{
var hash = seed;
for (var i = start; i < start + size; i++)
hash = (hash >> 8) ^ table[buffer[i] ^ hash & 0xff];
return hash;
}
static byte[] UInt32ToBigEndianBytes(UInt32 uint32)
{
var result = BitConverter.GetBytes(uint32);
if (BitConverter.IsLittleEndian)
Array.Reverse(result);
return result;
}
}
thanks and any help can be appreciated.
What I have tried:
Following links i have tried but not get Exact hash vales
Crc32 using HashAlgorithm
Crc32.cs source code in C# .NET
CRC Security C#
CRC C#