Skip to content

Commit 854c84d

Browse files
committed
v1.5.2 add CRC32
1 parent b4cf604 commit 854c84d

File tree

4 files changed

+164
-13
lines changed

4 files changed

+164
-13
lines changed

src/Senparc.CO2NET/Helpers/Files/FileHelper.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,24 @@ public static string GetFileHash(string filePath, string type = "SHA1", bool toU
169169
/// 获取文件的 HASH 值
170170
/// </summary>
171171
/// <param name="stream"></param>
172-
/// <param name="type">SHA1 或 MD5,必须为大写</param>
172+
/// <param name="type">SHA1 或 MD5 或 CRC32,必须为大写</param>
173173
/// <param name="toUpper">是否返回大写结果,true:大写,false:小写</param>
174174
/// <param name="encoding">默认为:utf8</param>
175-
public static string GetFileHash(Stream stream, string type = "SHA1", bool toUpper = true, Encoding encoding = null)
175+
public static string GetFileHash(Stream stream, string type = "SHA1", bool toUpper = true)
176176
{
177177
switch (type)
178178
{
179179
case "SHA1":
180180
{
181-
return EncryptHelper.GetSha1(stream, toUpper, encoding);
181+
return EncryptHelper.GetSha1(stream, toUpper);
182182
}
183183
case "MD5":
184184
{
185-
return EncryptHelper.GetMD5(stream, toUpper, encoding);
185+
return EncryptHelper.GetMD5(stream, toUpper);
186+
}
187+
case "CRC32":
188+
{
189+
return EncryptHelper.GetCrc32(stream, toUpper);
186190
}
187191
default:
188192
throw new ArgumentOutOfRangeException(nameof(type));
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Senparc.CO2NET.Helpers
8+
{
9+
/// <summary>
10+
/// CRC32 算法的实现
11+
/// </summary>
12+
internal class Crc32 : System.Security.Cryptography.HashAlgorithm
13+
{
14+
public const uint DefaultPolynomial = 0xedb88320;
15+
public const uint DefaultSeed = 0xffffffff;
16+
private uint hash;
17+
private uint seed;
18+
private uint[] table;
19+
private static uint[] defaultTable;
20+
public Crc32()
21+
{
22+
table = InitializeTable(DefaultPolynomial);
23+
seed = DefaultSeed;
24+
Initialize();
25+
}
26+
public Crc32(uint polynomial, uint seed)
27+
{
28+
table = InitializeTable(polynomial);
29+
this.seed = seed;
30+
Initialize();
31+
}
32+
public override void Initialize()
33+
{
34+
hash = seed;
35+
}
36+
protected override void HashCore(byte[] buffer, int start, int length)
37+
{
38+
hash = CalculateHash(table, hash, buffer, start, length);
39+
}
40+
protected override byte[] HashFinal()
41+
{
42+
byte[] hashBuffer = uintToBigEndianBytes(~hash);
43+
this.HashValue = hashBuffer;
44+
return hashBuffer;
45+
}
46+
public static uint Compute(byte[] buffer)
47+
{
48+
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
49+
}
50+
public static uint Compute(uint seed, byte[] buffer)
51+
{
52+
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
53+
}
54+
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
55+
{
56+
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
57+
}
58+
private static uint[] InitializeTable(uint polynomial)
59+
{
60+
if (polynomial == DefaultPolynomial && defaultTable != null)
61+
{
62+
return defaultTable;
63+
}
64+
uint[] createTable = new uint[256];
65+
for (int i = 0; i < 256; i++)
66+
{
67+
uint entry = (uint)i;
68+
for (int j = 0; j < 8; j++)
69+
{
70+
if ((entry & 1) == 1)
71+
entry = (entry >> 1) ^ polynomial;
72+
else
73+
entry = entry >> 1;
74+
}
75+
createTable[i] = entry;
76+
}
77+
if (polynomial == DefaultPolynomial)
78+
{
79+
defaultTable = createTable;
80+
}
81+
return createTable;
82+
}
83+
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
84+
{
85+
uint crc = seed;
86+
for (int i = start; i < size; i++)
87+
{
88+
unchecked
89+
{
90+
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
91+
}
92+
}
93+
return crc;
94+
}
95+
private byte[] uintToBigEndianBytes(uint x)
96+
{
97+
return new byte[] { (byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) };
98+
}
99+
}
100+
}

src/Senparc.CO2NET/Helpers/Strings/EncryptHelper.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ and limitations under the License.
4444
修改描述:v5.0.0 引入 Senparc.CO2NET
4545
4646
修改标识:Senparc - 20210831
47-
修改描述:v1.5.1 增加和丰富 EncryptHelper 中加密方法(SHA1、AesGcmDecrypt)
47+
修改描述:v1.5.1 增加和丰富 EncryptHelper 中加密方法(SHA1、AesGcmDecrypt、CRC32
4848
4949
----------------------------------------------------------------*/
5050

5151

52+
using Senparc.CO2NET.HttpUtility;
5253
using System;
5354
using System.Collections.Generic;
5455
using System.IO;
5556
using System.IO.Pipes;
5657
using System.Linq;
58+
using System.Runtime.InteropServices.ComTypes;
5759
using System.Security.Cryptography;
5860
using System.Text;
5961

@@ -134,9 +136,8 @@ public static string GetSha1(string encypStr, bool toUpper = true, Encoding enco
134136
/// </summary>
135137
/// <param name="stream">流</param>
136138
/// <param name="toUpper">是否返回大写结果,true:大写,false:小写</param>
137-
/// <param name="encoding">编码</param>
138139
/// <returns></returns>
139-
public static string GetSha1(Stream stream, bool toUpper = true, Encoding encoding = null)
140+
public static string GetSha1(Stream stream, bool toUpper = true)
140141
{
141142
stream.Seek(0, SeekOrigin.Begin);
142143

@@ -285,12 +286,10 @@ public static string GetMD5(string encypStr, string charset = "utf-8")
285286
/// 获取MD5签名结果
286287
/// </summary>
287288
/// <param name="stream">Stream</param>
288-
/// <param name="encoding">默认为:utf8</param>
289289
/// <param name="toUpper">是否返回大写结果,true:大写,false:小写</param>
290290
/// <returns></returns>
291-
public static string GetMD5(Stream stream, bool toUpper = true, Encoding encoding = null)
291+
public static string GetMD5(Stream stream, bool toUpper = true)
292292
{
293-
encoding ??= Encoding.UTF8;
294293
stream.Position = 0;
295294

296295
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
@@ -319,6 +318,54 @@ public static string GetLowerMD5(string encypStr, Encoding encoding)
319318

320319
#endregion
321320

321+
#region CRC32
322+
323+
/// <summary>
324+
///
325+
/// </summary>
326+
/// <param name="encypStr">需要加密的字符串</param>
327+
/// <param name="toUpper">是否返回大写结果,true:大写,false:小写</param>
328+
/// <param name="encoding"></param>
329+
/// <returns></returns>
330+
public static string GetCrc32(string encypStr, bool toUpper = true, Encoding encoding = null)
331+
{
332+
encoding ??= Encoding.UTF8;
333+
Crc32 calculator = new Crc32();
334+
byte[] buffer = calculator.ComputeHash(encoding.GetBytes(encypStr));
335+
calculator.Clear();
336+
//将字节数组转换成十六进制的字符串形式
337+
StringBuilder sb = new StringBuilder();
338+
for (int i = 0; i < buffer.Length; i++)
339+
{
340+
sb.Append(buffer[i].ToString("x2"));
341+
}
342+
343+
return toUpper ? toUpper.ToString().ToUpper() : sb.ToString();
344+
}
345+
346+
/// <summary>
347+
/// 获取 CRC32 加密字符串
348+
/// </summary>
349+
/// <param name="encypStr">需要加密的字符串</param>
350+
/// <param name="toUpper">是否返回大写结果,true:大写,false:小写</param>
351+
/// <returns></returns>
352+
public static string GetCrc32(Stream stream, bool toUpper = true)
353+
{
354+
Crc32 calculator = new Crc32();
355+
byte[] buffer = calculator.ComputeHash(stream);
356+
calculator.Clear();
357+
//将字节数组转换成十六进制的字符串形式
358+
StringBuilder sb = new StringBuilder();
359+
for (int i = 0; i < buffer.Length; i++)
360+
{
361+
sb.Append(buffer[i].ToString("x2"));
362+
}
363+
364+
return toUpper ? toUpper.ToString().ToUpper() : sb.ToString();
365+
}
366+
367+
#endregion
368+
322369
#region AES - CBC
323370

324371
/// <summary>

src/Senparc.CO2NET/Senparc.CO2NET.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;netstandard2.1</TargetFrameworks>
4-
<Version>1.5.1-preview3</Version>
4+
<Version>1.5.2-preview3</Version>
55
<LangVersion>8.0</LangVersion>
66
<AssemblyName>Senparc.CO2NET</AssemblyName>
77
<RootNamespace>Senparc.CO2NET</RootNamespace>
@@ -109,8 +109,8 @@
109109
v1.3.201 更新 Senparc.CO2NET.HttpUtility.Get.Download() 方法,修正 filename 判断正则表达式
110110
v1.4.400 修复 Download 方法 bug
111111

112-
v1.5.1
113-
1、增加和丰富 EncryptHelper 中加密方法(SHA1、AesGcmDecrypt)
112+
v1.5.2
113+
1、增加和丰富 EncryptHelper 中加密方法(SHA1、AesGcmDecrypt、CRC32
114114
2、添加 FileHelper 中 GetFileHash 方法
115115
</PackageReleaseNotes>
116116
<RepositoryUrl>https://github.com/Senparc/Senparc.CO2NET</RepositoryUrl>

0 commit comments

Comments
 (0)