-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathCrc32CAlgorithm.cs
More file actions
191 lines (174 loc) · 7.21 KB
/
Copy pathCrc32CAlgorithm.cs
File metadata and controls
191 lines (174 loc) · 7.21 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Security.Cryptography;
namespace Force.Crc32
{
/// <summary>
/// Implementation of CRC-32C (Castagnoli).
/// This class supports several convenient static methods returning the CRC as UInt32.
/// </summary>
public class Crc32CAlgorithm : HashAlgorithm
{
private uint _currentCrc;
private readonly bool _isBigEndian = true;
/// <summary>
/// Initializes a new instance of the <see cref="Crc32CAlgorithm"/> class.
/// </summary>
public Crc32CAlgorithm()
{
#if !NETCORE13
HashSizeValue = 32;
#endif
}
/// <summary>
/// Initializes a new instance of the <see cref="Crc32CAlgorithm"/> class.
/// </summary>
/// <param name="isBigEndian">Should return bytes result as big endian or little endian</param>
public Crc32CAlgorithm(bool isBigEndian = true)
: this()
{
_isBigEndian = isBigEndian;
}
/// <summary>
/// Computes CRC-32C from multiple buffers.
/// Call this method multiple times to chain multiple buffers.
/// </summary>
/// <param name="initial">
/// Initial CRC value for the algorithm. It is zero for the first buffer.
/// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method.
/// </param>
/// <param name="input">Input buffer with data to be checksummed.</param>
/// <param name="offset">Offset of the input data within the buffer.</param>
/// <param name="length">Length of the input data in the buffer.</param>
/// <returns>Accumulated CRC-32C of all buffers processed so far.</returns>
public static uint Append(uint initial, byte[] input, int offset, int length)
{
if (input == null)
throw new ArgumentNullException("input");
if (offset < 0 || length < 0 || offset + length > input.Length)
throw new ArgumentOutOfRangeException("length");
return AppendInternal(initial, input, offset, length);
}
/// <summary>
/// Computes CRC-32C from multiple buffers.
/// Call this method multiple times to chain multiple buffers.
/// </summary>
/// <param name="initial">
/// Initial CRC value for the algorithm. It is zero for the first buffer.
/// Subsequent buffers should have their initial value set to CRC value returned by previous call to this method.
/// </param>
/// <param name="input">Input buffer containing data to be checksummed.</param>
/// <returns>Accumulated CRC-32C of all buffers processed so far.</returns>
public static uint Append(uint initial, byte[] input)
{
if (input == null)
throw new ArgumentNullException("input");
return AppendInternal(initial, input, 0, input.Length);
}
/// <summary>
/// Computes CRC-32C from input buffer.
/// </summary>
/// <param name="input">Input buffer with data to be checksummed.</param>
/// <param name="offset">Offset of the input data within the buffer.</param>
/// <param name="length">Length of the input data in the buffer.</param>
/// <returns>CRC-32C of the data in the buffer.</returns>
public static uint Compute(byte[] input, int offset, int length)
{
return Append(0, input, offset, length);
}
/// <summary>
/// Computes CRC-32C from input buffer.
/// </summary>
/// <param name="input">Input buffer containing data to be checksummed.</param>
/// <returns>CRC-32C of the buffer.</returns>
public static uint Compute(byte[] input)
{
return Append(0, input);
}
/// <summary>
/// Computes CRC-32C from input buffer and writes it after end of data (buffer should have 4 bytes reserved space for it). Can be used in conjunction with <see cref="IsValidWithCrcAtEnd(byte[],int,int)"/>
/// </summary>
/// <param name="input">Input buffer with data to be checksummed.</param>
/// <param name="offset">Offset of the input data within the buffer.</param>
/// <param name="length">Length of the input data in the buffer.</param>
/// <returns>CRC-32C of the data in the buffer.</returns>
public static uint ComputeAndWriteToEnd(byte[] input, int offset, int length)
{
if (length + 4 > input.Length)
throw new ArgumentOutOfRangeException("length", "Length of data should be less than array length - 4 bytes of CRC data");
var crc = Append(0, input, offset, length);
var r = offset + length;
input[r] = (byte)crc;
input[r + 1] = (byte)(crc >> 8);
input[r + 2] = (byte)(crc >> 16);
input[r + 3] = (byte)(crc >> 24);
return crc;
}
/// <summary>
/// Computes CRC-32C from input buffer - 4 bytes and writes it as last 4 bytes of buffer. Can be used in conjunction with <see cref="IsValidWithCrcAtEnd(byte[])"/>
/// </summary>
/// <param name="input">Input buffer with data to be checksummed.</param>
/// <returns>CRC-32C of the data in the buffer.</returns>
public static uint ComputeAndWriteToEnd(byte[] input)
{
if (input.Length < 4)
throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least");
return ComputeAndWriteToEnd(input, 0, input.Length - 4);
}
/// <summary>
/// Validates correctness of CRC-32C data in source buffer with assumption that CRC-32C data located at end of buffer in reverse bytes order. Can be used in conjunction with <see cref="ComputeAndWriteToEnd(byte[],int,int)"/>
/// </summary>
/// <param name="input">Input buffer with data to be checksummed.</param>
/// <param name="offset">Offset of the input data within the buffer.</param>
/// <param name="lengthWithCrc">Length of the input data in the buffer with CRC-32C bytes.</param>
/// <returns>Is checksum valid.</returns>
public static bool IsValidWithCrcAtEnd(byte[] input, int offset, int lengthWithCrc)
{
return Append(0, input, offset, lengthWithCrc) == 0x48674BC7;
}
/// <summary>
/// Validates correctness of CRC-32C data in source buffer with assumption that CRC-32C data located at end of buffer in reverse bytes order. Can be used in conjunction with <see cref="ComputeAndWriteToEnd(byte[],int,int)"/>
/// </summary>
/// <param name="input">Input buffer with data to be checksummed.</param>
/// <returns>Is checksum valid.</returns>
public static bool IsValidWithCrcAtEnd(byte[] input)
{
if (input.Length < 4)
throw new ArgumentOutOfRangeException("input", "Input array should be 4 bytes at least");
return Append(0, input, 0, input.Length) == 0x48674BC7;
}
/// <summary>
/// Resets internal state of the algorithm. Used internally.
/// </summary>
public override void Initialize()
{
_currentCrc = 0;
}
/// <summary>
/// Appends CRC-32C from given buffer
/// </summary>
protected override void HashCore(byte[] input, int offset, int length)
{
_currentCrc = AppendInternal(_currentCrc, input, offset, length);
}
/// <summary>
/// Computes CRC-32C from <see cref="HashCore"/>
/// </summary>
protected override byte[] HashFinal()
{
if (_isBigEndian)
return new[] { (byte)(_currentCrc >> 24), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 8), (byte)_currentCrc };
else
return new[] { (byte)_currentCrc, (byte)(_currentCrc >> 8), (byte)(_currentCrc >> 16), (byte)(_currentCrc >> 24) };
}
private static readonly SafeProxyC _proxy = new SafeProxyC();
private static uint AppendInternal(uint initial, byte[] input, int offset, int length)
{
if (length > 0)
{
return _proxy.Append(initial, input, offset, length);
}
else
return initial;
}
}
}