Skip to content

Instantly share code, notes, and snippets.

@aynurin
Forked from breezhang/crc32.cs
Last active December 19, 2015 01:09
Show Gist options
  • Save aynurin/5874138 to your computer and use it in GitHub Desktop.
Save aynurin/5874138 to your computer and use it in GitHub Desktop.
using System.Security.Cryptography;
namespace MediaMetadata.Contribs.damieng
{
/// <summary>
/// Crc32 Implementation by damieng, http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
/// </summary>
public class Crc32 : HashAlgorithm
{
public const uint DefaultPolynomial = 0xedb88320;
public const uint DefaultSeed = 0xffffffff;
private uint _hash;
private readonly uint _seed;
private readonly uint[] _table;
private static uint[] _defaultTable;
public Crc32()
{
_table = InitializeTable(DefaultPolynomial);
_seed = DefaultSeed;
UpdateSettings();
}
public Crc32(uint polynomial, uint seed)
{
_table = InitializeTable(polynomial);
_seed = seed;
UpdateSettings();
}
public override void Initialize()
{
UpdateSettings();
}
private void UpdateSettings()
{
_hash = _seed;
}
protected override void HashCore(byte[] buffer, int start, int length)
{
_hash = CalculateHash(_table, _hash, buffer, start, length);
}
protected override byte[] HashFinal()
{
var hashBuffer = uintToBigEndianBytes(~_hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
public override int HashSize
{
get { return 32; }
}
public static uint Compute(byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
}
public static uint Compute(uint seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
}
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
private static uint[] InitializeTable(uint polynomial)
{
if (polynomial == DefaultPolynomial && _defaultTable != null)
return _defaultTable;
var createTable = new uint[256];
for (int i = 0; i < 256; i++)
{
var entry = (uint)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;
}
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
{
var crc = seed;
for (var i = start; i < size; i++)
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
return crc;
}
private byte[] uintToBigEndianBytes(uint x)
{
return new[] {
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff),
(byte)((x >> 8) & 0xff),
(byte)(x & 0xff)
};
}
}
}
Crc32 crc32 = new Crc32();
String hashs = String.Empty;
using (FileStream fs = File.Open(@"paths", FileMode.Open))
foreach (byte b in crc32.ComputeHash(fs)) hashs += b.ToString("x2").ToLower();
Console.WriteLine("CRC-32 is {0}", hashs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment