diff options
author | sr55 <[email protected]> | 2016-03-12 16:19:53 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2016-03-12 16:20:22 +0000 |
commit | 39613ae76e39b5a8ddaae50aa5d5665796acc726 (patch) | |
tree | 5f07ea0e79de1929e2e73037ee9f23fa9d634d91 /win/CS/HandBrakeWPF/Services/UpdateService.cs | |
parent | 1a972f68a412ae06b000930a189bb06bcb5ba6e6 (diff) |
WinGui: Add SHA1 Verification of the update downloads and don't start the installer if the hash check fails.
Diffstat (limited to 'win/CS/HandBrakeWPF/Services/UpdateService.cs')
-rw-r--r-- | win/CS/HandBrakeWPF/Services/UpdateService.cs | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/win/CS/HandBrakeWPF/Services/UpdateService.cs b/win/CS/HandBrakeWPF/Services/UpdateService.cs index f49836b6a..732193ac4 100644 --- a/win/CS/HandBrakeWPF/Services/UpdateService.cs +++ b/win/CS/HandBrakeWPF/Services/UpdateService.cs @@ -12,6 +12,8 @@ namespace HandBrakeWPF.Services using System;
using System.IO;
using System.Net;
+ using System.Security.Cryptography;
+ using System.Text;
using System.Threading;
using HandBrake.ApplicationServices.Utilities;
@@ -125,6 +127,7 @@ namespace HandBrakeWPF.Services DownloadFile = reader.DownloadFile,
Build = reader.Build,
Version = reader.Version,
+ ExpectedSHA1Hash = reader.Hash
};
callback(info2);
@@ -142,13 +145,16 @@ namespace HandBrakeWPF.Services /// <param name="url">
/// The url.
/// </param>
+ /// <param name="expectedSha1Hash">
+ /// The expected Sha 1 Hash.
+ /// </param>
/// <param name="completed">
/// The complete.
/// </param>
/// <param name="progress">
/// The progress.
/// </param>
- public void DownloadFile(string url, Action<DownloadStatus> completed, Action<DownloadStatus> progress)
+ public void DownloadFile(string url, string expectedSha1Hash, Action<DownloadStatus> completed, Action<DownloadStatus> progress)
{
ThreadPool.QueueUserWorkItem(
delegate
@@ -172,11 +178,9 @@ namespace HandBrakeWPF.Services int bytesSize;
byte[] downBuffer = new byte[2048];
- long flength = 0;
while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
localStream.Write(downBuffer, 0, bytesSize);
- flength = localStream.Length;
progress(new DownloadStatus { BytesRead = localStream.Length, TotalBytes = fileSize});
}
@@ -184,21 +188,45 @@ namespace HandBrakeWPF.Services localStream.Close();
completed(
- flength != fileSize
+ GetSHA1(tempPath) != expectedSha1Hash
? new DownloadStatus
{
WasSuccessful = false,
- Message = "Partial Download. File is Incomplete. Please Retry the download."
+ Message = "Download Failed. SHA1 Checksum Failed. Please visit the website to download this update."
}
: new DownloadStatus { WasSuccessful = true, Message = "Download Complete." });
}
catch (Exception exc)
{
- progress(new DownloadStatus { WasSuccessful = false, Exception = exc, Message = "Download Failed." });
+ progress(new DownloadStatus { WasSuccessful = false, Exception = exc, Message = "Download Failed. Please visit the website to download this update." });
}
});
}
+ /// <summary>
+ /// The get sh a 1.
+ /// </summary>
+ /// <param name="fileName">
+ /// The file name.
+ /// </param>
+ /// <returns>
+ /// The <see cref="string"/>.
+ /// </returns>
+ public static String GetSHA1(String fileName)
+ {
+ FileStream file = new FileStream(fileName, FileMode.Open);
+ SHA1 sha1 = new SHA1CryptoServiceProvider();
+ byte[] retVal = sha1.ComputeHash(file);
+ file.Close();
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < retVal.Length; i++)
+ {
+ sb.Append(retVal[i].ToString("x2"));
+ }
+ return sb.ToString();
+ }
+
#endregion
}
}
\ No newline at end of file |