-
Notifications
You must be signed in to change notification settings - Fork 3
/
CRC32Helper.cs
287 lines (271 loc) · 9.5 KB
/
CRC32Helper.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils.Helper.TXT;
namespace Utils.Helper.Encryption
{
/// <summary>
/// 循环沉余效验帮助类
/// 创建日期:2017年06月16日
/// </summary>
public class CRC32Helper
{
/// <summary>
/// CRC32 加密(8位小写)
/// </summary>
/// <param name="strPlaintext">明文</param>
/// <returns>CRC32密文(8位小写)</returns>
public static string CRC32Encrypt_8Lower(string strPlaintext)
{
try
{
Crc32 crc32Crypto = new Crc32();
byte[] bytes_crc32_in = Encoding.Default.GetBytes(strPlaintext);
byte[] bytes_crc32_out = crc32Crypto.ComputeHash(bytes_crc32_in);
string str_crc32_out = BitConverter.ToString(bytes_crc32_out);
str_crc32_out = str_crc32_out.Replace("-", "");
str_crc32_out = str_crc32_out.ToLower();
return str_crc32_out;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
/// <summary>
/// CRC32 加密(8位大写)
/// </summary>
/// <param name="strPlaintext">明文</param>
/// <returns>CRC32 密文(8位大写)</returns>
public static string CRC32Encrypt_8Upper(string strPlaintext)
{
try
{
Crc32 crc32Crypto = new Crc32();
byte[] bytes_crc32_in = Encoding.Default.GetBytes(strPlaintext);
byte[] bytes_crc32_out = crc32Crypto.ComputeHash(bytes_crc32_in);
string str_crc32_out = BitConverter.ToString(bytes_crc32_out);
str_crc32_out = str_crc32_out.Replace("-", "");
str_crc32_out = str_crc32_out.ToUpper();
return str_crc32_out;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
/// <summary>
/// 获取文件 CRC32 值(8位小写)
/// </summary>
/// <param name="strFilePath">文件路径</param>
/// <returns>文件 CRC32 值(8位小写)</returns>
public static string FileCRC32Encrypt_8Lower(string strFilePath)
{
try
{
string hashCRC32 = string.Empty;
// 检查文件是否存在,如果文件存在则进行计算,否则返回空值
if (File.Exists(strFilePath))
{
using (FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
{
// 计算文件的 CSC32 值
Crc32 calculator = new Crc32();
byte[] buffer = calculator.ComputeHash(fileStream);
calculator.Clear();
// 将字节数组转换成十六进制的字符串形式
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
stringBuilder.Append(buffer[i].ToString("x2"));
}
hashCRC32 = stringBuilder.ToString();
}
}
return hashCRC32;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
/// <summary>
/// 获取文件 CRC32 值(8位大写)
/// </summary>
/// <param name="strFilePath">文件路径</param>
/// <returns>文件 CRC32 值(8位大写)</returns>
public static string FileCRC32Encrypt_8Upper(string strFilePath)
{
try
{
String hashCRC32 = String.Empty;
// 检查文件是否存在,如果文件存在则进行计算,否则返回空值
if (File.Exists(strFilePath))
{
using (FileStream fileStream = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
{
// 计算文件的 CSC32 值
Crc32 calculator = new Crc32();
byte[] buffer = calculator.ComputeHash(fileStream);
calculator.Clear();
// 将字节数组转换成十六进制的字符串形式
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
stringBuilder.Append(buffer[i].ToString("X2"));
}
hashCRC32 = stringBuilder.ToString();
}
}
return hashCRC32;
}
catch (Exception ex)
{
TXTHelper.Logs(ex.ToString());
return string.Empty;
}
}
}
/// <summary>
/// 提供 CRC32 算法的实现
/// </summary>
public class Crc32 : System.Security.Cryptography.HashAlgorithm
{
/// <summary>
/// Default Polynomial
/// </summary>
public const UInt32 DefaultPolynomial = 0xedb88320;
/// <summary>
/// Default Seed
/// </summary>
public const UInt32 DefaultSeed = 0xffffffff;
private UInt32 hash;
private UInt32 seed;
private UInt32[] table;
private static UInt32[] defaultTable;
/// <summary>
/// Crc32
/// </summary>
public Crc32()
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
Initialize();
}
/// <summary>
/// Crc32
/// </summary>
/// <param name="polynomial"></param>
/// <param name="seed"></param>
public Crc32(UInt32 polynomial, UInt32 seed)
{
table = InitializeTable(polynomial);
this.seed = seed;
Initialize();
}
/// <summary>
/// 初始化
/// </summary>
public override void Initialize()
{
hash = seed;
}
/// <summary>
/// Hash Core
/// </summary>
/// <param name="buffer"></param>
/// <param name="start"></param>
/// <param name="length"></param>
protected override void HashCore(byte[] buffer, int start, int length)
{
hash = CalculateHash(table, hash, buffer, start, length);
}
/// <summary>
/// Hash Final
/// </summary>
/// <returns></returns>
protected override byte[] HashFinal()
{
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
/// <summary>
/// Compute
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static uint Compute(byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
}
/// <summary>
/// Compute
/// </summary>
/// <param name="seed"></param>
/// <param name="buffer"></param>
/// <returns></returns>
public static uint Compute(uint seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
}
/// <summary>
/// Compute
/// </summary>
/// <param name="polynomial"></param>
/// <param name="seed"></param>
/// <param name="buffer"></param>
/// <returns></returns>
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
private static uint[] InitializeTable(uint polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
{
return defaultTable;
}
uint[] createTable = new uint[256];
for (int i = 0; i < 256; i++)
{
uint entry = (uint)i;
for (int j = 0; j < 8; j++)
{
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
}
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
{
defaultTable = createTable;
}
return createTable;
}
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
{
uint crc = seed;
for (int i = start; i < size; i++)
{
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
}
return crc;
}
private byte[] UInt32ToBigEndianBytes(uint x)
{
return new byte[] { (byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) };
}
}
}