Skip to content

Commit 906cab3

Browse files
author
linzhijun
committed
添加 hmac_md5 公式支持
1 parent 7caa5e1 commit 906cab3

File tree

7 files changed

+70
-13
lines changed

7 files changed

+70
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ToolGood.Algorithm
44

55
ToolGood.Algorithm是一个功能强大、轻量级、兼容`Excel公式`的算法类库,旨在提高开发人员在不同业务场景中的生产力。
66

7-
WebAssembly版本,请看csharp文件夹下`ToolGood.Algorithm.WebAssembly`,`ToolGood.Algorithm2.WebTest` 两个项目。注意:不支持HmacMd5
7+
WebAssembly版本,请看csharp文件夹下`ToolGood.Algorithm.WebAssembly`,`ToolGood.Algorithm2.WebTest` 两个项目。
88

99
**适用场景:** 代码与算法分离,避免项目强制升级
1010

csharp/ToolGood.Algorithm2.WebTest/wwwroot/Test/test-csharp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@
107107

108108
dt = engine.TryEvaluate("Crc32('&=我中国人 >||')", null);
109109
assert.equal(dt, "60649EFF");
110-
//dt = engine.TryEvaluate("HmacMd5('&=我中国人 >||','12')", null);
111-
//assert.equal(dt, "CF3923196E21B1E270FD72B089B092BB"); //不支持HmacMd5
110+
dt = engine.TryEvaluate("HmacMd5('&=我中国人 >||','12')", null);
111+
assert.equal(dt, "CF3923196E21B1E270FD72B089B092BB"); //不支持HmacMd5
112112

113113

114114
dt = engine.TryEvaluate("HmacSha1('&=我中国人 >||','12')", null);
Binary file not shown.
Binary file not shown.

csharp/ToolGood.Algorithm2.WebTest/wwwroot/_framework/blazor.boot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"mainAssemblyName": "ToolGood.Algorithm.WebAssembly",
33
"resources": {
4-
"hash": "sha256-QI/VSIEswxA96S4yaYSkJ3CysXws5wLn7QRQ/1XPDbw=",
4+
"hash": "sha256-FiXkY7fbGhJ3skTFJvGGasmrUbxJ9dxndAcVs9a5uE0=",
55
"jsModuleNative": {
66
"dotnet.native.8.0.4.rkw194kvb3.js": "sha256-ju0+XWE0ihz554eKEj0HQ8Opa61xpbUIaX5/MNo8we4="
77
},
@@ -209,12 +209,12 @@
209209
"mscorlib.wasm": "sha256-VCfYgt1mcjITabSTosCDVZ+68ea3Z7kakTFaLNLG+18=",
210210
"netstandard.wasm": "sha256-YOZxxR9PM3Dltcijfb0Q4FD39DkWZbZ1e2ScbZDvWJ4=",
211211
"System.Private.CoreLib.wasm": "sha256-NrT1QkAzVU//2wgK9vXdq77lCGvLJ4IOZ+Aegesyanc=",
212-
"ToolGood.Algorithm.wasm": "sha256-iIpw7biw9MMOA/9L5trgPn9UCNnmi/TttCxGBptG2DU=",
213-
"ToolGood.Algorithm.WebAssembly.wasm": "sha256-/Yza5+IFkK1hJzI+qIsyEnVMaUenmi0kkWyGjKGqNCc="
212+
"ToolGood.Algorithm.wasm": "sha256-NDbHwdElZXxCEPm8FwjFH1HOPi5J/JaDPKi3q6cNaFU=",
213+
"ToolGood.Algorithm.WebAssembly.wasm": "sha256-qL9e/AbTVDT+yBJoIPnMGTwmHbbTq00oPCpX68uHtjY="
214214
},
215215
"pdb": {
216-
"ToolGood.Algorithm.pdb": "sha256-FNZNFzxsM7qnVhnpuPNAHfqH//i2gqiOdJASOBRlBbw=",
217-
"ToolGood.Algorithm.WebAssembly.pdb": "sha256-S97uA59sQzhfTjNw1ctoFPnLC/Rm5BhdCmkoZR2NVlQ="
216+
"ToolGood.Algorithm.pdb": "sha256-LLYQ5zCB53ruPKl6jtP08pZ0wTFDzjggRDnzCYnCaa0=",
217+
"ToolGood.Algorithm.WebAssembly.pdb": "sha256-VDrYvhH9hk8bctDanGJThOLKxR4g1vL3i5mk1ru7G5w="
218218
}
219219
},
220220
"cacheBootResources": true,

csharp/ToolGood.Algorithm2/Internals/Hash.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,18 @@ public static string GetSha512String(byte[] buffer)
151151
#region HMACMD5
152152
public static string GetHmacMd5String(byte[] buffer, string secret)
153153
{
154+
#if WebAssembly
154155
byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret ?? "");
156+
return MD5.hmac_md5(buffer, keyByte);
157+
#else
158+
byte[] keyByte = System.Text.Encoding.UTF8.GetBytes(secret ?? "");
155159
using (var hmacsha256 = new HMACMD5(keyByte)) {
156160
byte[] hashmessage = hmacsha256.ComputeHash(buffer);
157161
return BitConverter.ToString(hashmessage).Replace("-", "");
158162
}
163+
#endif
164+
165+
159166
}
160167
#endregion
161168

csharp/ToolGood.Algorithm2/Internals/MD5.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,21 @@ public static string ArrayToHexString(byte[] array, bool uppercase)
277277
}
278278
public static string MDString(byte[] c)
279279
{
280-
byte[] b = new byte[c.Length];
281-
for (int i = 0; i < c.Length; i++) {
282-
b[i] = (byte)c[i];
283-
}
284-
byte[] digest = MD5Array(b);
280+
//byte[] b = new byte[c.Length];
281+
//for (int i = 0; i < c.Length; i++) {
282+
// b[i] = (byte)c[i];
283+
//}
284+
byte[] digest = MD5Array(c);
285285
return ArrayToHexString(digest, true);
286286
}
287+
public static byte[] ComputeHash(byte[] c)
288+
{
289+
//byte[] b = new byte[c.Length];
290+
//for (int i = 0; i < c.Length; i++) {
291+
// b[i] = (byte)c[i];
292+
//}
293+
return MD5Array(c);
294+
}
287295

288296
//public static string MDString(string message)
289297
//{
@@ -296,6 +304,48 @@ public static string MDString(byte[] c)
296304
// return ArrayToHexString(digest, false);
297305
//}
298306

307+
308+
public static string hmac_md5(byte[] b_tmp1, byte[] source)
309+
{
310+
byte[] b_tmp;
311+
string szRet = string.Empty;
312+
313+
byte[] digest = new byte[512];
314+
byte[] k_ipad = new byte[64];
315+
byte[] k_opad = new byte[64];
316+
for (int i = 0; i < 64; i++) {
317+
k_ipad[i] = 0 ^ 0x36;
318+
k_opad[i] = 0 ^ 0x5c;
319+
}
320+
if (source.Length > 64) {
321+
source = ComputeHash(source);
322+
}
323+
for (int i = 0; i < source.Length; i++) {
324+
k_ipad[i] = (byte)(source[i] ^ 0x36);
325+
k_opad[i] = (byte)(source[i] ^ 0x5c);
326+
}
327+
328+
//b_tmp1 = System.Text.ASCIIEncoding.ASCII.GetBytes(timespan);
329+
b_tmp = adding(k_ipad, b_tmp1);
330+
digest = ComputeHash(b_tmp);
331+
b_tmp = adding(k_opad, digest);
332+
digest = ComputeHash(b_tmp);
333+
// for (int i = 0; i < digest.Length; i++)
334+
// {
335+
//outdigest = System.Text.ASCIIEncoding.ASCII.GetString(digest);//[i].ToString();//
336+
//
337+
return ArrayToHexString(digest, true); // }
338+
}
339+
/***
340+
* * 填充byte
341+
***/
342+
public static byte[] adding(byte[] a, byte[] b)
343+
{
344+
byte[] c = new byte[a.Length + b.Length];
345+
a.CopyTo(c, 0);
346+
b.CopyTo(c, a.Length);
347+
return c;
348+
}
299349
}
300350
}
301351

0 commit comments

Comments
 (0)