]> granicus.if.org Git - handbrake/commitdiff
WinGui: Add SHA1 Verification of the update downloads and don't start the installer...
authorsr55 <sr55.hb@outlook.com>
Sat, 12 Mar 2016 16:19:53 +0000 (16:19 +0000)
committersr55 <sr55.hb@outlook.com>
Sat, 12 Mar 2016 16:20:22 +0000 (16:20 +0000)
win/CS/HandBrakeWPF/Model/UpdateCheckInformation.cs
win/CS/HandBrakeWPF/Services/Interfaces/IUpdateService.cs
win/CS/HandBrakeWPF/Services/UpdateService.cs
win/CS/HandBrakeWPF/Utilities/AppcastReader.cs
win/CS/HandBrakeWPF/ViewModels/OptionsViewModel.cs

index bc49a2ee3fa01ab3bb42e532cc1ff50ec57b371d..61c2bd36e8247ec642b236abe7b20c5a5c038015 100644 (file)
@@ -53,5 +53,10 @@ namespace HandBrakeWPF.Model
         /// Gets or sets the error that occurred, if any. This will be null if no error occured.\r
         /// </summary>\r
         public Exception Error { get; set; }\r
+\r
+        /// <summary>\r
+        /// Gets or sets the expected sh a 1 hash.\r
+        /// </summary>\r
+        public string ExpectedSHA1Hash { get; set; }\r
     }\r
 }\r
index cd1b50e9ffffc347ff32a96373f976c34be9618f..571d5c9314a38b57fde364c3d5b763ba95763fa2 100644 (file)
@@ -40,12 +40,15 @@ namespace HandBrakeWPF.Services.Interfaces
         /// <param name="url">\r
         /// The url.\r
         /// </param>\r
+        /// <param name="expectedSHA1Hash">\r
+        /// The expected SHA-1 Hash.\r
+        /// </param>\r
         /// <param name="completed">\r
         /// The complete.\r
         /// </param>\r
         /// <param name="progress">\r
         /// The progress.\r
         /// </param>\r
-        void DownloadFile(string url, Action<DownloadStatus> completed, Action<DownloadStatus> progress);\r
+        void DownloadFile(string url, string expectedSHA1Hash, Action<DownloadStatus> completed, Action<DownloadStatus> progress);\r
     }\r
 }\r
index f49836b6af1a818000672cf76f98981ae5d36f6b..732193ac4a8b2823cd604f53a504b9a67f03154f 100644 (file)
@@ -12,6 +12,8 @@ namespace HandBrakeWPF.Services
     using System;\r
     using System.IO;\r
     using System.Net;\r
+    using System.Security.Cryptography;\r
+    using System.Text;\r
     using System.Threading;\r
 \r
     using HandBrake.ApplicationServices.Utilities;\r
@@ -125,6 +127,7 @@ namespace HandBrakeWPF.Services
                                 DownloadFile = reader.DownloadFile,\r
                                 Build = reader.Build,\r
                                 Version = reader.Version,\r
+                                ExpectedSHA1Hash = reader.Hash\r
                             };\r
 \r
                         callback(info2);\r
@@ -142,13 +145,16 @@ namespace HandBrakeWPF.Services
         /// <param name="url">\r
         /// The url.\r
         /// </param>\r
+        /// <param name="expectedSha1Hash">\r
+        /// The expected Sha 1 Hash.\r
+        /// </param>\r
         /// <param name="completed">\r
         /// The complete.\r
         /// </param>\r
         /// <param name="progress">\r
         /// The progress.\r
         /// </param>\r
-        public void DownloadFile(string url, Action<DownloadStatus> completed, Action<DownloadStatus> progress)\r
+        public void DownloadFile(string url, string expectedSha1Hash, Action<DownloadStatus> completed, Action<DownloadStatus> progress)\r
         {\r
             ThreadPool.QueueUserWorkItem(\r
                delegate\r
@@ -172,11 +178,9 @@ namespace HandBrakeWPF.Services
                        int bytesSize;\r
                        byte[] downBuffer = new byte[2048];\r
 \r
-                       long flength = 0;\r
                        while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
                        {\r
                            localStream.Write(downBuffer, 0, bytesSize);\r
-                           flength = localStream.Length;\r
                            progress(new DownloadStatus { BytesRead = localStream.Length, TotalBytes = fileSize});\r
                        }\r
 \r
@@ -184,21 +188,45 @@ namespace HandBrakeWPF.Services
                        localStream.Close();\r
 \r
                        completed(\r
-                           flength != fileSize\r
+                           GetSHA1(tempPath) != expectedSha1Hash\r
                                ? new DownloadStatus\r
                                    {\r
                                        WasSuccessful = false,\r
-                                       Message = "Partial Download. File is Incomplete. Please Retry the download."\r
+                                       Message = "Download Failed.  SHA1 Checksum Failed. Please visit the website to download this update."\r
                                    }\r
                                : new DownloadStatus { WasSuccessful = true, Message = "Download Complete." });\r
                    }\r
                    catch (Exception exc)\r
                    {\r
-                       progress(new DownloadStatus { WasSuccessful = false, Exception = exc, Message = "Download Failed." });\r
+                       progress(new DownloadStatus { WasSuccessful = false, Exception = exc, Message = "Download Failed. Please visit the website to download this update." });\r
                    }\r
                });\r
         }\r
 \r
+        /// <summary>\r
+        /// The get sh a 1.\r
+        /// </summary>\r
+        /// <param name="fileName">\r
+        /// The file name.\r
+        /// </param>\r
+        /// <returns>\r
+        /// The <see cref="string"/>.\r
+        /// </returns>\r
+        public static String GetSHA1(String fileName)\r
+        {\r
+            FileStream file = new FileStream(fileName, FileMode.Open);\r
+            SHA1 sha1 = new SHA1CryptoServiceProvider();\r
+            byte[] retVal = sha1.ComputeHash(file);\r
+            file.Close();\r
+\r
+            StringBuilder sb = new StringBuilder();\r
+            for (int i = 0; i < retVal.Length; i++)\r
+            {\r
+                sb.Append(retVal[i].ToString("x2"));\r
+            }\r
+            return sb.ToString();\r
+        }\r
+\r
         #endregion\r
     }\r
 }
\ No newline at end of file
index 49d7f4714f6e6333c55af3e9f827b9cdef243be2..b534ee8c60f2a8db20b793713f1b0411fdcda1bc 100644 (file)
@@ -39,6 +39,11 @@ namespace HandBrakeWPF.Utilities
         /// </summary>\r
         public string DownloadFile { get; private set; }\r
 \r
+        /// <summary>\r
+        /// Gets the hash for verifying the download completed correctly.\r
+        /// </summary>\r
+        public string Hash { get; private set; }\r
+\r
         /// <summary>\r
         /// Get the build information from the required appcasts. Run before accessing the public vars.\r
         /// </summary>\r
@@ -61,6 +66,7 @@ namespace HandBrakeWPF.Utilities
                 this.Version = verShort.ToString().Replace("sparkle:shortVersionString=", string.Empty).Replace(\r
                     "\"", string.Empty);\r
                 this.DownloadFile = nodeItem["windows"].InnerText;\r
+                this.Hash = nodeItem["windowsHash"].InnerText;  \r
                 this.DescriptionUrl = new Uri(nodeItem["sparkle:releaseNotesLink"].InnerText);\r
             }\r
             catch (Exception)\r
index 3176070d029026b93fba91a4d8f0aaff7b542811..69df5a635a63da9935f01ccc187e1ba300064620 100644 (file)
@@ -1124,7 +1124,7 @@ namespace HandBrakeWPF.ViewModels
         public void DownloadUpdate()\r
         {\r
             this.UpdateMessage = "Preparing for Update ...";\r
-            this.updateService.DownloadFile(this.updateInfo.DownloadFile, this.DownloadComplete, this.DownloadProgress);\r
+            this.updateService.DownloadFile(this.updateInfo.DownloadFile, this.updateInfo.ExpectedSHA1Hash, this.DownloadComplete, this.DownloadProgress);\r
         }\r
 \r
         /// <summary>\r
@@ -1412,10 +1412,13 @@ namespace HandBrakeWPF.ViewModels
         private void DownloadComplete(DownloadStatus info)\r
         {\r
             this.UpdateAvailable = false;\r
-            this.UpdateMessage = info.WasSuccessful ? Resources.OptionsViewModel_UpdateDownloaded : Resources.OptionsViewModel_UpdateFailed;\r
+            this.UpdateMessage = info.WasSuccessful ? Resources.OptionsViewModel_UpdateDownloaded : info.Message;\r
 \r
-            Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"));\r
-            Execute.OnUIThread(() => Application.Current.Shutdown());\r
+            if (info.WasSuccessful)\r
+            {\r
+                Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"));\r
+                Execute.OnUIThread(() => Application.Current.Shutdown());\r
+            }\r
         }\r
 \r
         /// <summary>\r