From 03c1eaad76b95449ee9656da3587cf8d933923e9 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 31 May 2019 16:52:44 +0000 Subject: [PATCH] Add a SHA1 check for downloaded packages. --- OpenRA.Mods.Common/ModContent.cs | 1 + .../Installation/DownloadPackageLogic.cs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/OpenRA.Mods.Common/ModContent.cs b/OpenRA.Mods.Common/ModContent.cs index fc3379ec8d..a57aeed55a 100644 --- a/OpenRA.Mods.Common/ModContent.cs +++ b/OpenRA.Mods.Common/ModContent.cs @@ -76,6 +76,7 @@ namespace OpenRA public readonly string Title; public readonly string URL; public readonly string MirrorList; + public readonly string SHA1; public readonly Dictionary Extract; public ModDownload(MiniYaml yaml) diff --git a/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackageLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackageLogic.cs index 6dea1a00a9..468ca9e2d4 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackageLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackageLogic.cs @@ -137,6 +137,37 @@ namespace OpenRA.Mods.Common.Widgets.Logic return; } + // Validate integrity + if (!string.IsNullOrEmpty(download.SHA1)) + { + getStatusText = () => "Verifying archive..."; + progressBar.Indeterminate = true; + + var archiveValid = false; + try + { + using (var stream = File.OpenRead(file)) + { + var archiveSHA1 = CryptoUtil.SHA1Hash(stream); + Log.Write("install", "Downloaded SHA1: " + archiveSHA1); + Log.Write("install", "Expected SHA1: " + download.SHA1); + + archiveValid = archiveSHA1 == download.SHA1; + } + } + catch (Exception e) + { + Log.Write("install", "SHA1 calculation failed: " + e.ToString()); + } + + if (!archiveValid) + { + onError("Archive validation failed"); + deleteTempFile(); + return; + } + } + // Automatically extract getStatusText = () => "Extracting..."; progressBar.Indeterminate = true;