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
DownloadFile = reader.DownloadFile,\r
Build = reader.Build,\r
Version = reader.Version,\r
+ ExpectedSHA1Hash = reader.Hash\r
};\r
\r
callback(info2);\r
/// <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
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
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
/// </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
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
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
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