-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrc32.cs
62 lines (55 loc) · 1.67 KB
/
Crc32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Linq;
using System.Security.Cryptography;
namespace com.clusterrr.Famicom.Containers
{
/// <summary>
/// CRC32 calculator
/// </summary>
internal class Crc32 : HashAlgorithm
{
private readonly uint[] table = new uint[256];
private uint crc = 0xFFFFFFFF;
public Crc32()
{
// Calculate table
uint poly = 0xedb88320;
uint temp;
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;
}
}
public override void Initialize()
{
}
public override bool CanReuseTransform => false;
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
while (cbSize > 0)
{
var pos = ibStart;
byte index = (byte)(((crc) & 0xff) ^ array[pos]);
crc = (crc >> 8) ^ table[index];
ibStart++;
cbSize--;
}
}
protected override byte[] HashFinal() => BitConverter.GetBytes(~crc).Reverse().ToArray();
public override bool CanTransformMultipleBlocks => true;
public override byte[] Hash => BitConverter.GetBytes(~crc).Reverse().ToArray();
public override int HashSize => 32;
}
}