public NotifyReadStream(Stream InInner, NotifyReadDelegate InNotifyRead) { Inner = InInner; NotifyRead = InNotifyRead; TotalRead = 0; }
static void DownloadFileAndVerifyHash(string Url, string ProxyUrl, string PackFileName, string ExpectedHash, NotifyReadDelegate NotifyRead) { // Create the web request WebRequest Request = WebRequest.Create(Url); if (String.IsNullOrEmpty(ProxyUrl)) { Request.Proxy = null; } else { Request.Proxy = new WebProxy(ProxyUrl); } // Get the response using (WebResponse Response = Request.GetResponse()) { // Download the file, decompressing and hashing it as we go SHA1Managed Hasher = new SHA1Managed(); using (FileStream OutputStream = File.OpenWrite(PackFileName)) { CryptoStream HashOutputStream = new CryptoStream(OutputStream, Hasher, CryptoStreamMode.Write); using (NotifyReadStream InputStream = new NotifyReadStream(Response.GetResponseStream(), NotifyRead)) { GZipStream DecompressedStream = new GZipStream(InputStream, CompressionMode.Decompress, true); DecompressedStream.CopyTo(HashOutputStream); } HashOutputStream.FlushFinalBlock(); } // Check the hash was what we expected string Hash = BitConverter.ToString(Hasher.Hash).ToLower().Replace("-", ""); if (Hash != ExpectedHash) { throw new InvalidDataException(String.Format("Incorrect hash for {0} - expected {1}, got {2}", Url, ExpectedHash, Hash)); } } }