-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCrcHash.cs
88 lines (72 loc) · 2.63 KB
/
CrcHash.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// ---------------------------------------------------------------------------------------------
// Copyright (c) 2021-2022, Jiaqi Liu. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// ---------------------------------------------------------------------------------------------
using System;
using System.Linq;
using System.Text;
namespace Cpk.Net
{
internal class CrcHash
{
private const uint CrcTableMax = 256;
private const uint Polynomial = 0x04C11DB7; // CRC seed
private const int GbkCodePage = 936; // GBK Encoding's code page
private static readonly uint[] CrcTable = new uint[CrcTableMax];
private static bool _initialized;
// generate the table of CRC remainders for all possible bytes
public void Init()
{
for (uint i = 0; i < CrcTableMax; i++)
{
var crcAccum = i << 24;
for (var j = 0; j < 8; j++)
{
if ((crcAccum & 0x80000000L) != 0)
crcAccum = (crcAccum << 1) ^ Polynomial;
else
crcAccum = (crcAccum << 1);
}
CrcTable[i] = crcAccum;
}
_initialized = true;
}
public uint ToCrc32Hash(string str)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return ToCrc32Hash(Encoding.GetEncoding(GbkCodePage).GetBytes(str));
}
public uint ToCrc32Hash(byte[] strBytes)
{
if (strBytes.Length == 0 || strBytes[0] == 0) return 0;
var data = strBytes.ToList();
// Append 0 to the end of the byte list if not
if (data[^1] != 0) data.Add(0);
if (!_initialized)
{
throw new Exception($"{nameof(CrcHash)} not initialized.");
}
var index = 0;
uint result = (uint)(data[index++] << 24);
if (data[index] != 0)
{
result |= (uint)(data[index++] << 16);
if(data[index] != 0)
{
result |= (uint)(data[index++] << 8);
if (data[index] != 0)
{
result |= data[index++];
}
}
}
result = ~result;
while (data[index] != 0)
{
result = (result << 8 | data[index]) ^ CrcTable[result >> 24];
index++;
}
return ~result;
}
}
}