Пример #1
0
        public void TestHashCalculation()
        {
            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))
                    {
                        hashStream.CopyTo(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))
                    {
                        randomReadStream.CopyTo(hashStream);
                    }
            Assert.AreEqual(hash, hashStream.HashResult.HashBytes);
        }