-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRC64.cs
57 lines (48 loc) · 1.39 KB
/
CRC64.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
// This code is based on: https://github.com/franklinwk/OpenIGTLink-Unity
// Modified by Alicia Pose Díez de la Lastra, from Universidad Carlos III de Madrid
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CRC64
{
private ulong[] _table;
private ulong CmTab(int index, ulong poly)
{
ulong retval = (ulong)index;
ulong topbit = (ulong)1L << (64 - 1);
ulong mask = 0xffffffffffffffffUL;
retval <<= (64 - 8);
for (int i = 0; i < 8; i++)
{
if ((retval & topbit) != 0)
retval = (retval << 1) ^ poly;
else
retval <<= 1;
}
return retval & mask;
}
private ulong[] GenStdCrcTable(ulong poly)
{
ulong[] table = new ulong[256];
for (var i = 0; i < 256; i++)
table[i] = CmTab(i, poly);
return table;
}
private ulong TableValue(ulong[] table, byte b, ulong crc)
{
return table[((crc >> 56) ^ b) & 0xffUL] ^ (crc << 8);
}
public void Init(ulong poly)
{
_table = GenStdCrcTable(poly);
}
public ulong Compute(byte[] bytes, ulong initial, ulong final)
{
ulong current = initial;
for (var i = 0; i < bytes.Length; i++)
{
current = TableValue(_table, bytes[i], current);
}
return current ^ final;
}
}