Skip to content

Commit fcbe9e6

Browse files
committed
New: CheckForUpdates to support CDN cache
1 parent 309e658 commit fcbe9e6

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

RZ.Base/RZRestAPIv2.cs

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Net;
88
using System.Net.Http;
99
using System.Net.Sockets;
10+
using System.Numerics;
11+
using System.Security.Cryptography;
1012
using System.Text;
1113
using System.Threading.Tasks;
1214
using System.Web.Script.Serialization;
@@ -333,9 +335,10 @@ public static List<AddSoftware> CheckForUpdate(List<AddSoftware> lSoftware, stri
333335
if (lSoftware.Count > 0)
334336
{
335337
JavaScriptSerializer ser = new JavaScriptSerializer();
336-
HttpContent oCont = new StringContent(ser.Serialize(lSoftware), Encoding.UTF8, "application/json");
337-
338-
var response = oClient.PostAsync(sURL + "/rest/v2/checkforupdate", oCont);
338+
string sSoftware = ser.Serialize(lSoftware);
339+
HttpContent oCont = new StringContent(sSoftware, Encoding.UTF8, "application/json");
340+
string sHash = CalculateMD5HashString(sSoftware);
341+
var response = oClient.PostAsync(sURL + $"/rest/v2/checkforupdate/{sHash}", oCont);
339342
response.Wait(120000); //2min max
340343
if (response.IsCompleted)
341344
{
@@ -353,6 +356,56 @@ public static List<AddSoftware> CheckForUpdate(List<AddSoftware> lSoftware, stri
353356
return new List<AddSoftware>();
354357
}
355358

359+
#region Hash
360+
private const string Digits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
361+
362+
private static byte[] CalculateMD5Hash(string input)
363+
{
364+
MD5 md5 = System.Security.Cryptography.MD5.Create();
365+
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
366+
byte[] hash = md5.ComputeHash(inputBytes);
367+
byte[] mhash = new byte[hash.Length + 2];
368+
hash.CopyTo(mhash, 2);
369+
//Add Multihash identifier
370+
mhash[0] = 0xD5; //MD5
371+
mhash[1] = Convert.ToByte(hash.Length); //Hash legth
372+
return mhash;
373+
}
374+
375+
private static string Encode58(byte[] data)
376+
{
377+
// Decode byte[] to BigInteger
378+
BigInteger intData = 0;
379+
for (int i = 0; i < data.Length; i++)
380+
{
381+
intData = intData * 256 + data[i];
382+
}
383+
384+
// Encode BigInteger to Base58 string
385+
string result = "";
386+
while (intData > 0)
387+
{
388+
int remainder = (int)(intData % 58);
389+
intData /= 58;
390+
result = Digits[remainder] + result;
391+
}
392+
393+
// Append `1` for each leading 0 byte
394+
for (int i = 0; i < data.Length && data[i] == 0; i++)
395+
{
396+
result = '1' + result;
397+
}
398+
399+
return result;
400+
}
401+
402+
public static string CalculateMD5HashString(string input)
403+
{
404+
return Encode58(CalculateMD5Hash(input));
405+
}
406+
407+
#endregion
408+
356409
}
357410

358411
public class GetSoftware

RuckZuck_Tool/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@
5252
// by using the '*' as shown below:
5353
// [assembly: AssemblyVersion("1.0.*")]
5454
[assembly: AssemblyVersion("1.7.0.*")]
55-
[assembly: AssemblyFileVersion("1.7.0.5")]
55+
[assembly: AssemblyFileVersion("1.7.0.6")]

RuckZuck_Tool/RuckZuck_Tool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
</Reference>
105105
<Reference Include="System.Net" />
106106
<Reference Include="System.Net.Http" />
107+
<Reference Include="System.Numerics" />
107108
<Reference Include="System.Printing" />
108109
<Reference Include="System.Runtime.Serialization" />
109110
<Reference Include="System.Security" />

0 commit comments

Comments
 (0)