public async Task TestHashCalculationAsync() { FileInfo fileInfo = new FileInfo(GetType().Assembly.Location); byte[] hash; using (FileStream fileStream = fileInfo.OpenRead()) using (HashAlgorithm hashCalculator = HashAlgorithm.Create(HashAlgorithmName.Name)) { hash = hashCalculator.ComputeHash(fileStream); } HashStream hashStream; using (FileStream fileStream = fileInfo.OpenRead()) using (RandomReadNumberStream randomReadStream = new RandomReadNumberStream(fileStream)) using (hashStream = new HashStream(randomReadStream, CryptoStreamMode.Read, HashAlgorithmName)) { await hashStream.CopyToAsync(Stream.Null); } Assert.AreEqual(hash, hashStream.HashResult.HashBytes); using (FileStream fileStream = fileInfo.OpenRead()) using (RandomReadNumberStream randomReadStream = new RandomReadNumberStream(fileStream)) using (hashStream = new HashStream(Stream.Null, CryptoStreamMode.Write, HashAlgorithmName)) { await randomReadStream.CopyToAsync(hashStream); } Assert.AreEqual(hash, hashStream.HashResult.HashBytes); }
public async Task TestNoStreamConstructor() { FileInfo fileInfo = new FileInfo(GetType().Assembly.Location); HashStream hashStream1, hashStream2; using (FileStream fileStream = fileInfo.OpenRead()) using (hashStream1 = new HashStream(fileStream, CryptoStreamMode.Read, HashAlgorithmName)) using (hashStream2 = new HashStream(HashAlgorithmName)) { await hashStream1.CopyToAsync(Stream.Null); fileStream.Seek(0, SeekOrigin.Begin); await fileStream.CopyToAsync(hashStream2); } Assert.AreEqual(hashStream1.HashResult, hashStream2.HashResult); }