forked from grinay/ProxyLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrc16.cs
More file actions
105 lines (94 loc) · 3.24 KB
/
Crc16.cs
File metadata and controls
105 lines (94 loc) · 3.24 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Author:
// Benton Stark <benton.stark@gmail.com>
//
// Copyright (c) 2016 Benton Stark
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Security.Cryptography;
namespace ProxyLib
{
/// <summary>
/// Compute standard CRC-16 hash value. This class
/// inherits from the standard .NET HashAlgorithm
/// class.
/// </summary>
public class Crc16 : HashAlgorithm
{
// constants
private const ushort Polynomial = 0xA001;
private const int HashTableSize = 256;
// hash lookup table
private ushort[] _table = new ushort[HashTableSize];
private ushort _crc = 0;
/// <summary>
/// Constructor
/// </summary>
public Crc16()
{
Initialize();
}
/// <summary>
/// Hash core override function.
/// </summary>
/// <param name="buffer">Data buffer to hash.</param>
/// <param name="offset">Offset value in the data buffer.</param>
/// <param name="count">Number of bytes to hash.</param>
protected override void HashCore(byte[] buffer, int offset, int count)
{
// computer the hash on the bytes given
for (int i = offset; i < count; i++)
{
// xor byte with existing crc value
byte index = (byte)(_crc ^ buffer[i]);
// update crc via xor with lookup table
_crc = (ushort)((_crc >> 8) ^ _table[index]);
}
}
/// <summary>
/// Hash final bytes. Nothing to do except return the hash result.
/// </summary>
/// <returns>Final hashed bytes.</returns>
protected override byte[] HashFinal()
{
return BitConverter.GetBytes(_crc);
}
/// <summary>
/// Initialize the CRC16 hashing function by building a
/// hash value table for fast lookup.
/// </summary>
public override void Initialize()
{
ushort value;
ushort temp;
// for each byte in the table loop
for (ushort i = 0; i < _table.Length; ++i)
{
value = 0;
temp = i;
// set the bits of each byte
for (byte j = 0; j < 8; ++j)
{
if (((value ^ temp) & 0x0001) != 0)
value = (ushort)((value >> 1) ^ Polynomial);
else
value >>= 1;
temp >>= 1;
}
_table[i] = value;
}
}
}
}