From 9ceb66a3d57b185e2fea914dddfb7ede5b659f0e Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Thu, 23 Jan 2025 13:40:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E6=96=87=E4=BB=B6hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Script/AppMain/Common/Helper.cs | 57 ++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/Helper.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/Helper.cs index 6d64427e..15c06d7c 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/Helper.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/Helper.cs @@ -1,8 +1,14 @@ -using System; +using NUnit.Framework; +using System; +using System.Collections.Generic; using System.IO; using System.IO.Compression; +using System.Linq; using System.Security.Cryptography; +using System.Security.Policy; using System.Text; +using Unity.Android.Gradle.Manifest; +using static UnityEngine.Analytics.IAnalytic; namespace AxibugEmuOnline.Client.Common { @@ -59,19 +65,56 @@ namespace AxibugEmuOnline.Client.Common } } } - public static string FileMD5Hash(byte[] data) + + static byte[] FileMD5HashByte(byte[] data) { using (var md5 = MD5.Create()) { using (var stream = new MemoryStream(data)) { - var hash = md5.ComputeHash(stream); - var sb = new StringBuilder(hash.Length * 2); - foreach (var b in hash) - sb.AppendFormat("{0:x2}", b); - return sb.ToString(); + return md5.ComputeHash(stream); } } } + + /// + /// 单个文件hash + /// + /// + /// + public static string FileMD5Hash(byte[] data) + { + byte[] hash = FileMD5HashByte(data); + var sb = new StringBuilder(hash.Length * 2); + foreach (var b in hash) + sb.AppendFormat("{0:x2}", b); + return sb.ToString(); + } + + /// + /// 多文件总hash (顺序不影响结果) + /// + /// + /// + public static string FileMD5Hash(List dataList) + { + string allhash = string.Empty; + List temp = new List(); + for (int i = 0; i < dataList.Count; i++) + { + if (dataList[i] == null) + continue; + temp.Add(FileMD5Hash(dataList[i])); + } + temp.Sort(); + var sb = new StringBuilder(); + for (int i = 0; i < temp.Count; i++) + { + sb.AppendLine(temp[i].ToString()); + } + + //这里用Ascll + return FileMD5Hash(Encoding.ASCII.GetBytes(sb.ToString())); + } } }