diff --git a/src/c#/GeneralUpdate.Api/Program.cs b/src/c#/GeneralUpdate.Api/Program.cs index 62732030b493e01262c211f7543f16f235c0d691..d1536828cee76099035e2ce89fde2703ce6c3c6b 100644 --- a/src/c#/GeneralUpdate.Api/Program.cs +++ b/src/c#/GeneralUpdate.Api/Program.cs @@ -36,7 +36,7 @@ app.MapPost("/push", async Task (HttpContext context) => app.MapGet("/versions/{clientType}/{clientVersion}/{clientAppKey}", (int clientType, string clientVersion, string clientAppKey, IUpdateService updateService) => { var versions = new List(); - var md5 = "9bf414990a67e74f11752d03f49b15d8";//生成好的更新包文件的MD5码,因为返回给客户端的时候需要同这个来验证是否可用 + var hash = "9bf414990a67e74f11752d03f49b15d8";//生成好的更新包文件的MD5码,因为返回给客户端的时候需要同这个来验证是否可用 var pubTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); string version = null; if (clientType == AppType.ClientApp) @@ -53,7 +53,7 @@ app.MapGet("/versions/{clientType}/{clientVersion}/{clientAppKey}", (int clientT } var url = $"http://192.168.50.203/update.zip";//更新包的下载地址 var name = "update"; - versions.Add(new VersionDTO(md5, pubTime, version, url, name)); + versions.Add(new VersionDTO(hash, pubTime, version, url, name)); return updateService.Update(clientType, clientVersion, version, clientAppKey, GetAppSecretKey(), false, versions); }); @@ -69,7 +69,7 @@ app.MapPost("/upload", async Task (HttpContext context, HttpRequest requ int.TryParse(contextReq.Form["clientType"], out int clientType); var version = contextReq.Form["clientType"].ToString(); var clientAppKey = contextReq.Form["clientAppKey"].ToString(); - var md5 = contextReq.Form["md5"].ToString(); + var hash = contextReq.Form["hash"].ToString(); if (!request.HasFormContentType) throw new Exception("ContentType was not included in the request !"); var form = await request.ReadFormAsync(); diff --git a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj index d7be444f28606dea1c5e7ba69e1e9fc5c4384c9a..15b2e58532cdbeff68f2dcb7e5a13af19d406c09 100644 --- a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj +++ b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj @@ -51,6 +51,7 @@ + @@ -113,13 +114,17 @@ + + + + + - @@ -209,6 +214,7 @@ + diff --git a/src/c#/GeneralUpdate.ClientCore/HashAlgorithms/.gitkeep b/src/c#/GeneralUpdate.ClientCore/HashAlgorithms/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/c#/GeneralUpdate.Core/Domain/DO/Assembler/VersionAssembler.cs b/src/c#/GeneralUpdate.Core/Domain/DO/Assembler/VersionAssembler.cs index a8ff88f086307805e072a922e4b5fd7450cacedc..867c325fa33cd341a418e7fee22de478f62acb85 100644 --- a/src/c#/GeneralUpdate.Core/Domain/DO/Assembler/VersionAssembler.cs +++ b/src/c#/GeneralUpdate.Core/Domain/DO/Assembler/VersionAssembler.cs @@ -17,7 +17,7 @@ namespace GeneralUpdate.Core.Domain.DO.Assembler public static VersionInfo ToDataObject(VersionConfigDO versionDO) { - return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.MD5, versionDO.Version, versionDO.Url); + return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.Hash, versionDO.Version, versionDO.Url); } } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/Domain/DO/VersionConfigDO.cs b/src/c#/GeneralUpdate.Core/Domain/DO/VersionConfigDO.cs index 8f10262bba749e88cdafacd70493609c796bf3a7..a9829bd6474a79e4f42240463eb2ba7c5be5237d 100644 --- a/src/c#/GeneralUpdate.Core/Domain/DO/VersionConfigDO.cs +++ b/src/c#/GeneralUpdate.Core/Domain/DO/VersionConfigDO.cs @@ -15,9 +15,9 @@ namespace GeneralUpdate.Core.Domain.DO public string Url { get; set; } /// - /// MD5 verification code + /// Hash verification code /// - public string MD5 { get; set; } + public string Hash { get; set; } /// /// Update the package name. @@ -44,17 +44,17 @@ namespace GeneralUpdate.Core.Domain.DO /// /// /// - /// + /// /// /// /// /// /// - public VersionConfigDO(string guid, string url, string mD5, string name, string format, string version, long pubTime) + public VersionConfigDO(string guid, string url, string hash, string name, string format, string version, long pubTime) { Guid = guid ?? throw new ArgumentNullException(nameof(guid)); Url = url ?? throw new ArgumentNullException(nameof(url)); - MD5 = mD5 ?? throw new ArgumentNullException(nameof(mD5)); + Hash = hash ?? throw new ArgumentNullException(nameof(hash)); Name = name ?? throw new ArgumentNullException(nameof(name)); Format = format ?? throw new ArgumentNullException(nameof(format)); Version = version ?? throw new ArgumentNullException(nameof(version)); diff --git a/src/c#/GeneralUpdate.Core/Domain/DTO/Assembler/VersionAssembler.cs b/src/c#/GeneralUpdate.Core/Domain/DTO/Assembler/VersionAssembler.cs index 0ff8e2ca68d90ef487b4fdad54a044e8b400e0f6..ac850e2b0bac3f01a481ff8b71728b4ed88d1d1e 100644 --- a/src/c#/GeneralUpdate.Core/Domain/DTO/Assembler/VersionAssembler.cs +++ b/src/c#/GeneralUpdate.Core/Domain/DTO/Assembler/VersionAssembler.cs @@ -17,7 +17,7 @@ namespace GeneralUpdate.Core.Domain.DTO.Assembler public static VersionInfo ToEntity(VersionDTO versionDTO) { - return new VersionInfo(versionDTO.PubTime, versionDTO.Name, versionDTO.MD5, versionDTO.Version, versionDTO.Url); + return new VersionInfo(versionDTO.PubTime, versionDTO.Name, versionDTO.Hash, versionDTO.Version, versionDTO.Url); } } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/Domain/DTO/VersionDTO.cs b/src/c#/GeneralUpdate.Core/Domain/DTO/VersionDTO.cs index e6e205edff0d4e8d766b31f05989abadc9968328..6f070e7b4aea69075399847a9ca5baaa283bd858 100644 --- a/src/c#/GeneralUpdate.Core/Domain/DTO/VersionDTO.cs +++ b/src/c#/GeneralUpdate.Core/Domain/DTO/VersionDTO.cs @@ -2,16 +2,16 @@ { public class VersionDTO { - public VersionDTO(string md5, long pubTime, string version, string url, string name) + public VersionDTO(string hash, long pubTime, string version, string url, string name) { - MD5 = md5; + Hash = hash; PubTime = pubTime; Version = version; Url = url; Name = name; } - public string MD5 { get; set; } + public string Hash { get; set; } public long PubTime { get; set; } diff --git a/src/c#/GeneralUpdate.Core/Domain/Entity/VersionInfo.cs b/src/c#/GeneralUpdate.Core/Domain/Entity/VersionInfo.cs index 06a2a0291668078633af8917f5f278813667e1df..e1d611b71e38df322a26ab57423ae94194fa71e2 100644 --- a/src/c#/GeneralUpdate.Core/Domain/Entity/VersionInfo.cs +++ b/src/c#/GeneralUpdate.Core/Domain/Entity/VersionInfo.cs @@ -7,11 +7,11 @@ namespace GeneralUpdate.Core.Domain.Entity public VersionInfo() { } - public VersionInfo(long pubTime, string name, string mD5, string version, string url) + public VersionInfo(long pubTime, string name, string hash, string version, string url) { PubTime = pubTime; Name = name ?? throw new ArgumentNullException(nameof(name)); - MD5 = mD5 ?? throw new ArgumentNullException(nameof(mD5)); + Hash = hash ?? throw new ArgumentNullException(nameof(hash)); Version = version ?? throw new ArgumentNullException(nameof(version)); Url = url ?? throw new ArgumentNullException(nameof(Url)); if (!IsURL(Url)) throw new Exception($"Illegal url {nameof(Url)}"); @@ -30,7 +30,7 @@ namespace GeneralUpdate.Core.Domain.Entity /// /// Compare and verify with the downloaded update package. /// - public string MD5 { get; set; } + public string Hash { get; set; } /// /// The version number. diff --git a/src/c#/GeneralUpdate.Core/Domain/Enum/ProgressType.cs b/src/c#/GeneralUpdate.Core/Domain/Enum/ProgressType.cs index d3de1be3574e53bdc69224a0923896511b2e0ed8..027268be8a48c3a1c2000e97306e2269123cc409 100644 --- a/src/c#/GeneralUpdate.Core/Domain/Enum/ProgressType.cs +++ b/src/c#/GeneralUpdate.Core/Domain/Enum/ProgressType.cs @@ -38,8 +38,8 @@ Patch, /// - /// MD5 code + /// Hash code /// - MD5 + Hash } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/Domain/PO/Assembler/VersionAssembler.cs b/src/c#/GeneralUpdate.Core/Domain/PO/Assembler/VersionAssembler.cs index 2f61c820327d9ffa02a45e69d2fd3c5d711adf7f..4b7e0082a5e666e3ce121e18f1f85140efabb73c 100644 --- a/src/c#/GeneralUpdate.Core/Domain/PO/Assembler/VersionAssembler.cs +++ b/src/c#/GeneralUpdate.Core/Domain/PO/Assembler/VersionAssembler.cs @@ -17,7 +17,7 @@ namespace GeneralUpdate.Core.Domain.PO.Assembler public static VersionInfo ToDataObject(VersionPO versionDO) { - return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.MD5, versionDO.Version, versionDO.Url); + return new VersionInfo(versionDO.PubTime, versionDO.Name, versionDO.Hash, versionDO.Version, versionDO.Url); } } } diff --git a/src/c#/GeneralUpdate.Core/Domain/PO/VersionPO.cs b/src/c#/GeneralUpdate.Core/Domain/PO/VersionPO.cs index 9cb598295ab145b5c3b63a4c32cecb0a7896422b..43f9161711a72435190f435c1f7a01727b9bab09 100644 --- a/src/c#/GeneralUpdate.Core/Domain/PO/VersionPO.cs +++ b/src/c#/GeneralUpdate.Core/Domain/PO/VersionPO.cs @@ -18,7 +18,7 @@ /// /// Compare and verify with the downloaded update package. /// - public string MD5 { get; set; } + public string Hash { get; set; } /// /// The version number. diff --git a/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs b/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs new file mode 100644 index 0000000000000000000000000000000000000000..bd75ad69fc5ea58d4f9436524caa963249080f64 --- /dev/null +++ b/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs @@ -0,0 +1,28 @@ +锘縰sing System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace GeneralUpdate.Core.HashAlgorithms +{ + public abstract class HashAlgorithmBase + { + public string ComputeHash(string fileName) + { + using (var hashAlgorithm = GetHashAlgorithm()) + { + using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) + { + var dataArray = GetHashAlgorithm().ComputeHash(file); + var stringBuilder = new StringBuilder(); + for (int i = 0; i < dataArray.Length; i++) + { + stringBuilder.Append(dataArray[i].ToString("x2")); + } + return stringBuilder.ToString(); + } + } + } + + protected abstract HashAlgorithm GetHashAlgorithm(); + } +} \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/HashAlgorithms/Md5HashAlgorithm.cs b/src/c#/GeneralUpdate.Core/HashAlgorithms/Md5HashAlgorithm.cs new file mode 100644 index 0000000000000000000000000000000000000000..d7bbd0bcb17bf92a7085eaca9a0b8f0b0a3d415f --- /dev/null +++ b/src/c#/GeneralUpdate.Core/HashAlgorithms/Md5HashAlgorithm.cs @@ -0,0 +1,9 @@ +锘縰sing System.Security.Cryptography; + +namespace GeneralUpdate.Core.HashAlgorithms +{ + public class Md5HashAlgorithm : HashAlgorithmBase + { + protected override HashAlgorithm GetHashAlgorithm()=> MD5.Create(); + } +} diff --git a/src/c#/GeneralUpdate.Core/HashAlgorithms/Sha1HashAlgorithm.cs b/src/c#/GeneralUpdate.Core/HashAlgorithms/Sha1HashAlgorithm.cs new file mode 100644 index 0000000000000000000000000000000000000000..1013e2f7fcbef2dbf4f5439b6fe05fa5690dd8c3 --- /dev/null +++ b/src/c#/GeneralUpdate.Core/HashAlgorithms/Sha1HashAlgorithm.cs @@ -0,0 +1,9 @@ +锘縰sing System.Security.Cryptography; + +namespace GeneralUpdate.Core.HashAlgorithms +{ + public class Sha1HashAlgorithm : HashAlgorithmBase + { + protected override HashAlgorithm GetHashAlgorithm()=> new SHA1Managed(); + } +} diff --git a/src/c#/GeneralUpdate.Core/HashAlgorithms/Sha256HashAlgorithm.cs b/src/c#/GeneralUpdate.Core/HashAlgorithms/Sha256HashAlgorithm.cs new file mode 100644 index 0000000000000000000000000000000000000000..e88290a3e93ba40c9f77a455acc65f5b7f29d7de --- /dev/null +++ b/src/c#/GeneralUpdate.Core/HashAlgorithms/Sha256HashAlgorithm.cs @@ -0,0 +1,9 @@ +锘縰sing System.Security.Cryptography; + +namespace GeneralUpdate.Core.HashAlgorithms +{ + public class Sha256HashAlgorithm : HashAlgorithmBase + { + protected override HashAlgorithm GetHashAlgorithm()=> SHA256.Create(); + } +} diff --git a/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ConfigMiddleware.cs b/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ConfigMiddleware.cs index a3920e0dfe191241d2e52e8f618a281d23981fe3..ddbc8e2b3e80cee892f4526f1c2e350aac7c26db 100644 --- a/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ConfigMiddleware.cs +++ b/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ConfigMiddleware.cs @@ -16,7 +16,7 @@ namespace GeneralUpdate.Core.Pipelines.Middleware { try { - EventManager.Instance.Dispatch>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.MD5, "Update configuration file ...")); + EventManager.Instance.Dispatch>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.Hash, "Update configuration file ...")); await ConfigFactory.Instance.Deploy(); var node = stack.Pop(); if (node != null) await node.Next.Invoke(context, stack); diff --git a/src/c#/GeneralUpdate.Core/Pipelines/Middleware/MD5Middleware.cs b/src/c#/GeneralUpdate.Core/Pipelines/Middleware/HashMiddleware.cs similarity index 62% rename from src/c#/GeneralUpdate.Core/Pipelines/Middleware/MD5Middleware.cs rename to src/c#/GeneralUpdate.Core/Pipelines/Middleware/HashMiddleware.cs index aa43da468c5fabcd9f45f9f3e668fea774a4c116..9757268ded07bfaba96f278901e25cfc4a6e0514 100644 --- a/src/c#/GeneralUpdate.Core/Pipelines/Middleware/MD5Middleware.cs +++ b/src/c#/GeneralUpdate.Core/Pipelines/Middleware/HashMiddleware.cs @@ -2,24 +2,24 @@ using GeneralUpdate.Core.Events; using GeneralUpdate.Core.Events.CommonArgs; using GeneralUpdate.Core.Events.MultiEventArgs; +using GeneralUpdate.Core.HashAlgorithms; using GeneralUpdate.Core.Pipelines.Context; -using GeneralUpdate.Core.Utils; using System; using System.Threading.Tasks; namespace GeneralUpdate.Core.Pipelines.Middleware { - public class MD5Middleware : IMiddleware + public class HashMiddleware : IMiddleware { public async Task InvokeAsync(BaseContext context, MiddlewareStack stack) { Exception exception = null; try { - EventManager.Instance.Dispatch>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.MD5, "Verify file MD5 code ...")); + EventManager.Instance.Dispatch>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.Hash, "Verify file MD5 code ...")); var version = context.Version; - bool isVerify = VerifyFileMd5(context.ZipfilePath, version.MD5); - if (!isVerify) exception = new Exception($"The update package MD5 code is inconsistent ! version-{version.Version} MD5-{version.MD5} ."); + bool isVerify = VerifyFileHash(context.ZipfilePath, version.Hash); + if (!isVerify) exception = new Exception($"The update package hash code is inconsistent ! version-{version.Version} hash-{version.Hash} ."); var node = stack.Pop(); if (node != null) await node.Next.Invoke(context, stack); } @@ -29,10 +29,11 @@ namespace GeneralUpdate.Core.Pipelines.Middleware } } - private bool VerifyFileMd5(string fileName, string md5) + private bool VerifyFileHash(string fileName, string hash) { - var packetMD5 = FileUtil.GetFileMD5(fileName); - return md5.ToUpper().Equals(packetMD5.ToUpper()); + var hashAlgorithm = new Sha256HashAlgorithm(); + var hashSha256 = hashAlgorithm.ComputeHash(fileName); + return string.Equals(hash, hashSha256, StringComparison.OrdinalIgnoreCase); } } } \ No newline at end of file diff --git a/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ZipMiddleware.cs b/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ZipMiddleware.cs index 6395ca406c7a19dbf6adfa0f1036f4e260087e54..9cfee0753ca5c77a9de3fe6a99226f85fcb19c59 100644 --- a/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ZipMiddleware.cs +++ b/src/c#/GeneralUpdate.Core/Pipelines/Middleware/ZipMiddleware.cs @@ -20,7 +20,7 @@ namespace GeneralUpdate.Core.Pipelines.Middleware EventManager.Instance.Dispatch>(this, new MultiDownloadProgressChangedEventArgs(context.Version, ProgressType.Updatefile, "In the unzipped file ...")); var version = context.Version; bool isUnzip = UnZip(context); - if (!isUnzip) throw exception = new Exception($"Unzip file failed , Version-{version.Version} MD5-{version.MD5} !"); + if (!isUnzip) throw exception = new Exception($"Unzip file failed , Version-{version.Version} MD5-{version.Hash} !"); //await ConfigFactory.Instance.Scan(context.SourcePath, context.TargetPath); var node = stack.Pop(); if (node != null) await node.Next.Invoke(context, stack); diff --git a/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs b/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs index 228a1fcde8c651bc8daf97fd42e952b9e5fc7ea0..0606ad05e3dbbbc2c114ce47ccc98671f3a19e3d 100644 --- a/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs +++ b/src/c#/GeneralUpdate.Core/Strategys/PlatformWindows/WindowsStrategy.cs @@ -60,7 +60,7 @@ namespace GeneralUpdate.Core.Strategys.PlatformWindows .Build(); var pipelineBuilder = new PipelineBuilder(context). - UseMiddleware(). + UseMiddleware(). UseMiddleware(). UseMiddleware(); await pipelineBuilder.Launch(); @@ -146,7 +146,7 @@ namespace GeneralUpdate.Core.Strategys.PlatformWindows var startTime = DateTime.UtcNow; while (DateTime.UtcNow - startTime < timeout) { - Thread.Sleep(2 * 1000); + Thread.Sleep(3 * 1000); if (!process.HasExited) { callbackAction?.Invoke(applicationPath); diff --git a/src/c#/GeneralUpdate.Core/Utils/FileUtil.cs b/src/c#/GeneralUpdate.Core/Utils/FileUtil.cs index b778ed5bc98f3a058d0cd01c30e510cf27d5eb8b..ca431c3709a341e69ed81e061be246e75a487fc7 100644 --- a/src/c#/GeneralUpdate.Core/Utils/FileUtil.cs +++ b/src/c#/GeneralUpdate.Core/Utils/FileUtil.cs @@ -3,34 +3,11 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Security.Cryptography; -using System.Text; namespace GeneralUpdate.Core.Utils { public static class FileUtil { - public static string GetFileMD5(string fileName) - { - try - { - var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); - var md5 = new MD5CryptoServiceProvider(); - var retVal = md5.ComputeHash(file); - file.Close(); - var sb = new StringBuilder(); - for (var i = 0; i < retVal.Length; i++) - { - sb.Append(retVal[i].ToString("x2")); - } - return sb.ToString(); - } - catch (Exception) - { - return string.Empty; - } - } - public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, bool isOverWrite, Action action) { diff --git a/src/c#/GeneralUpdate.Differential/Config/Cache/ConfigEntity.cs b/src/c#/GeneralUpdate.Differential/Config/Cache/ConfigEntity.cs index b312bcc3221164eae146e1a41e4fba4eaac2834d..c81f6d712c74a1d700c68810a1d210e267c6b261 100644 --- a/src/c#/GeneralUpdate.Differential/Config/Cache/ConfigEntity.cs +++ b/src/c#/GeneralUpdate.Differential/Config/Cache/ConfigEntity.cs @@ -10,9 +10,9 @@ namespace GeneralUpdate.Differential.Config.Cache public string Name { get; set; } /// - /// file md5 code . + /// file hash code . /// - public string MD5 { get; set; } + public string Hash { get; set; } /// /// configuation file content. diff --git a/src/c#/GeneralUpdate.Differential/Config/Cache/ICache.cs b/src/c#/GeneralUpdate.Differential/Config/Cache/ICache.cs index 435092df25c0b2d869b986ad8a74a8c8d7700550..a0e11d471129c6bba7df2cb9ff2768a8bb042d1b 100644 --- a/src/c#/GeneralUpdate.Differential/Config/Cache/ICache.cs +++ b/src/c#/GeneralUpdate.Differential/Config/Cache/ICache.cs @@ -9,21 +9,21 @@ /// /// add cache. /// - /// file md5 + /// file hash /// configuration file content. void TryAdd(string key, TEntity entity); /// /// remove cache. /// - /// file md5 + /// file hash /// bool TryRemove(string key); /// /// get cache configuration file content. /// - /// file md5 + /// file hash /// configuration file content. TEntity TryGet(string key); } diff --git a/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs b/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs index 36674089993939859c0416210c84736f7e95c630..dd840c902ae733ec95e9158c58dce0b858fe4e14 100644 --- a/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs +++ b/src/c#/GeneralUpdate.Differential/Config/ConfigFactory.cs @@ -1,4 +1,5 @@ -锘縰sing GeneralUpdate.Core.Utils; +锘縰sing GeneralUpdate.Core.HashAlgorithms; +using GeneralUpdate.Core.Utils; using GeneralUpdate.Differential.Common; using GeneralUpdate.Differential.Config.Cache; using GeneralUpdate.Differential.Config.Handles; @@ -74,8 +75,9 @@ namespace GeneralUpdate.Differential.Config { var value = cacheItem.Value; if (value == null) continue; - var fileMD5 = FileUtil.GetFileMD5(value.OldPath); - var oldEntity = await Handle(value.OldPath, fileMD5); + var hashAlgorithm = new Sha256HashAlgorithm(); + var hash = hashAlgorithm.ComputeHash(value.OldPath); + var oldEntity = await Handle(value.OldPath, hash); await InitHandle(value.Handle).Write(oldEntity, value); } Dispose(); @@ -179,9 +181,10 @@ namespace GeneralUpdate.Differential.Config { foreach (var file in files) { - var fileMD5 = FileUtil.GetFileMD5(file); - var entity = await Handle(file, fileMD5); - _configCache.TryAdd(fileMD5, entity); + var hashAlgorithm = new Sha256HashAlgorithm(); + var hash = hashAlgorithm.ComputeHash(file); + var entity = await Handle(file, hash); + _configCache.TryAdd(hash, entity); } _configCache.Build(); } @@ -195,15 +198,15 @@ namespace GeneralUpdate.Differential.Config /// Process file content. /// /// file path - /// md5 + /// hash /// - private async Task Handle(string file, string fileMD5) + private async Task Handle(string file, string hash) { var entity = new ConfigEntity(); entity.Path = file; entity.Name = Path.GetFileName(file); entity.OldPath = Path.Combine(_appPath, entity.Name); - entity.MD5 = fileMD5; + entity.Hash = hash; entity.Handle = ToEnum(file); entity.Content = await InitHandle(entity.Handle).Read(file); return entity; diff --git a/src/c#/GeneralUpdate.Differential/ContentProvider/FileNode.cs b/src/c#/GeneralUpdate.Differential/ContentProvider/FileNode.cs index 7ce3179a532d0ff0d9d453e88f718e968a151ac0..1aa883c43b96891f8e6ff986d844f3f81618c3a5 100644 --- a/src/c#/GeneralUpdate.Differential/ContentProvider/FileNode.cs +++ b/src/c#/GeneralUpdate.Differential/ContentProvider/FileNode.cs @@ -14,7 +14,7 @@ namespace GeneralUpdate.Differential.ContentProvider public string Path { get; set; } - public string MD5 { get; set; } + public string Hash { get; set; } public FileNode Left { get; set; } @@ -127,16 +127,16 @@ namespace GeneralUpdate.Differential.ContentProvider } /// - /// Compare tree nodes equally by MD5 and file names. + /// Compare tree nodes equally by Hash and file names. /// /// /// public override bool Equals(object obj) { - if (obj == null) return false; //throw new ArgumentNullException(nameof(obj)); + if (obj == null) return false; var tempNode = obj as FileNode; if (tempNode == null) throw new ArgumentException(nameof(tempNode)); - return MD5.Equals(tempNode.MD5) && Name.Equals(tempNode.Name); + return Hash.Equals(tempNode.Hash) && Name.Equals(tempNode.Name); } public override int GetHashCode() => base.GetHashCode(); diff --git a/src/c#/GeneralUpdate.Differential/ContentProvider/FileProvider.cs b/src/c#/GeneralUpdate.Differential/ContentProvider/FileProvider.cs index 0eac735a1943a206ea9665342858cf32ea48d06c..2ddf0170cc6e78bbeed11a8e0804fb2a26a9acab 100644 --- a/src/c#/GeneralUpdate.Differential/ContentProvider/FileProvider.cs +++ b/src/c#/GeneralUpdate.Differential/ContentProvider/FileProvider.cs @@ -1,9 +1,11 @@ -锘縰sing GeneralUpdate.Core.Utils; +锘縰sing GeneralUpdate.Core.HashAlgorithms; +using GeneralUpdate.Core.Utils; using GeneralUpdate.Differential.Common; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices.ComTypes; using System.Threading; using System.Threading.Tasks; @@ -77,9 +79,11 @@ namespace GeneralUpdate.Differential.ContentProvider foreach (var subPath in Directory.GetFiles(path)) { if (IsMatchBlacklist(subPath)) continue; - var md5 = FileUtil.GetFileMD5(subPath); + + var hashAlgorithm = new Sha256HashAlgorithm(); + var hash = hashAlgorithm.ComputeHash(subPath); var subFileInfo = new FileInfo(subPath); - resultFiles.Add(new FileNode() { Id = GetId(), Path = path, Name = subFileInfo.Name, MD5 = md5, FullName = subFileInfo.FullName }); + resultFiles.Add(new FileNode() { Id = GetId(), Path = path, Name = subFileInfo.Name, Hash = hash, FullName = subFileInfo.FullName }); } foreach (var subPath in Directory.GetDirectories(path)) { diff --git a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs index 707f246c9437e3d94383423fe04acdc66e257bc5..9cd9d36f95e8a681b94d14308de76470a0c3e7d7 100644 --- a/src/c#/GeneralUpdate.Differential/DifferentialCore.cs +++ b/src/c#/GeneralUpdate.Differential/DifferentialCore.cs @@ -1,4 +1,5 @@ -锘縰sing GeneralUpdate.Core.Utils; +锘縰sing GeneralUpdate.Core.HashAlgorithms; +using GeneralUpdate.Core.Utils; using GeneralUpdate.Differential.Binary; using GeneralUpdate.Differential.Common; using GeneralUpdate.Differential.ContentProvider; @@ -168,9 +169,10 @@ namespace GeneralUpdate.Differential if (deleteListJson != null) { var deleteFiles = FileUtil.ReadJsonFile>(deleteListJson.FullName); + var hashAlgorithm = new Sha256HashAlgorithm(); foreach (var file in deleteFiles) { - var resultFile = oldFiles.FirstOrDefault(i => FileUtil.GetFileMD5(i.FullName).ToUpper().Equals(file.MD5.ToUpper())); + var resultFile = oldFiles.FirstOrDefault(i => string.Equals(hashAlgorithm.ComputeHash(i.FullName), file.Hash, StringComparison.OrdinalIgnoreCase)); if (resultFile == null) continue; if (File.Exists(resultFile.FullName)) File.Delete(resultFile.FullName); } diff --git a/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj b/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj index 34f38b733070b01fc3f2efdbfa1148114e1d5c58..3b3de6bbfbe5e5d08a267b0a6650a46fdbae332a 100644 --- a/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj +++ b/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj @@ -22,6 +22,10 @@ + + + + @@ -38,6 +42,7 @@ + diff --git a/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs b/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs index 9303526261d6df6416afc464ab71735821963f20..2a6178c368ba4e0b41bb0c4f023e12a38e18a960 100644 --- a/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs +++ b/src/c#/GeneralUpdate.Maui.OSS/Platforms/Android/Strategy.cs @@ -62,7 +62,7 @@ namespace GeneralUpdate.Maui.OSS => EventManager.Instance.Dispatch>(this, new OSSDownloadArgs(readLength, totalLength))); var apkFile = new Java.IO.File(file); if (!apkFile.Exists()) throw new Java.IO.FileNotFoundException(jsonPath); - if (!versionConfig.MD5.Equals(GetFileMD5(apkFile, 64))) throw new Exception("The apk MD5 value does not match !"); + if (!versionConfig.Hash.Equals(GetFileMD5(apkFile, 64))) throw new Exception("The apk MD5 value does not match !"); //5.Launch the apk to install. var intent = new Android.Content.Intent(Android.Content.Intent.ActionView); diff --git a/src/c#/GeneralUpdate.MiniBowl/GeneralUpdate.MiniBowl.csproj b/src/c#/GeneralUpdate.MiniBowl/GeneralUpdate.MiniBowl.csproj index 412d83da4bd8ec619c17e0d2adebb99f4c32f456..afc6d21bd9279e57d13f2536dde88a30a89841b2 100644 --- a/src/c#/GeneralUpdate.MiniBowl/GeneralUpdate.MiniBowl.csproj +++ b/src/c#/GeneralUpdate.MiniBowl/GeneralUpdate.MiniBowl.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/src/c#/GeneralUpdate.MiniBowl/Jbos/CheckClientJob.cs b/src/c#/GeneralUpdate.MiniBowl/Jbos/CheckClientJob.cs new file mode 100644 index 0000000000000000000000000000000000000000..653d463916cda2886f92ffca34a8c4939c58dd83 --- /dev/null +++ b/src/c#/GeneralUpdate.MiniBowl/Jbos/CheckClientJob.cs @@ -0,0 +1,33 @@ +锘縰sing Quartz; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GeneralUpdate.MiniBowl.Jbos +{ + internal class CheckClientJob : IJob + { + public async Task Execute(IJobExecutionContext context) + { + if (!IsChromeRunning()) + { + Process.Start("chrome.exe"); + } + + await Task.CompletedTask; + } + + private bool IsChromeRunning() + { + foreach (var process in Process.GetProcesses()) + { + if (process.ProcessName.ToLower().Contains("chrome")) + return true; + } + return false; + } + } +} diff --git a/src/c#/GeneralUpdate.MiniBowl/MiniBowlBootstrap.cs b/src/c#/GeneralUpdate.MiniBowl/MiniBowlBootstrap.cs index 0f1103c9d8aec3c0d92bae61316948bc03d1c91b..beac631e049d8758824ad1daeb2bad793834807e 100644 --- a/src/c#/GeneralUpdate.MiniBowl/MiniBowlBootstrap.cs +++ b/src/c#/GeneralUpdate.MiniBowl/MiniBowlBootstrap.cs @@ -8,7 +8,7 @@ namespace GeneralUpdate.MiniBowl { internal class MiniBowlBootstrap { - void Config() { } + void WithConfig() { } void Launch() { } } diff --git a/src/c#/TestMD5/UnitTest1.cs b/src/c#/TestMD5/UnitTest1.cs index 857da1145c80936839f975e56d418a635fcb6d06..9e6a35d436981658b736eae3b6e43d749da1f33c 100644 --- a/src/c#/TestMD5/UnitTest1.cs +++ b/src/c#/TestMD5/UnitTest1.cs @@ -9,12 +9,6 @@ namespace TestMD5 { try { - string path = "F:\temp\target\testpacket.zip"; - var md5 = FileUtil.GetFileMD5(path); - if (string.IsNullOrWhiteSpace(md5)) - { - Assert.Fail(); - } } catch (Exception ex) {