public void TestHashStreamDisposed()
 {
     using (HashStream hs = new HashStream(new SHA256Managed()))
     {
         hs.Close();
         hs.Close();                 //<- fails, already closed and/or disposed
     }
 }
Beispiel #2
0
            }             // func ReadStreamData

            private static (string hashName, byte[] hashValue) CopyData(bool shouldDeflate, Stream srcStream, byte[] buf, int readed, Stream dstStream)
            {
                // pack destination stream
                var dst = shouldDeflate
                                        ? new GZipStream(dstStream, CompressionMode.Compress, true)
                                        : dstStream;

                using (var dstHashStream = new HashStream(dst, HashStreamDirection.Write, false, SHA256.Create()))
                {
                    try
                    {
                        // copy stream into file
                        dstHashStream.Write(buf, 0, readed);
                        srcStream.CopyTo(dst);
                    }
                    finally
                    {
                        dstHashStream.Flush();
                        dstHashStream.Dispose();
                    }

                    dstHashStream.Close();

                    return("SHA2_256", dstHashStream.HashSum);
                }
            }             // func CopyData
        public void TestHashStreamWrite()
        {
            Random r = new Random();

            byte[][] test =
                new byte[][]
            {
                new byte[300],
                new byte[1],
                new byte[500],
                new byte[11],
                new byte[1],
                new byte[1000],
            };
            using (HashStream hs = new HashStream(new SHA256Managed()))
                using (MemoryStream ms = new MemoryStream())
                    using (HashStream hsWrap = new HashStream(new SHA256Managed(), ms))
                    {
                        Assert.IsTrue(hs.CanWrite);
                        long len = 0;
                        foreach (byte[] bytes in test)
                        {
                            len += bytes.Length;
                            r.NextBytes(bytes);
                            hsWrap.Write(bytes, 0, bytes.Length);
                            hs.Write(bytes, 0, bytes.Length);
                        }
                        for (int i = 0; i < 5; i++)
                        {
                            len += 1;
                            byte val = (byte)r.Next(255);
                            hsWrap.WriteByte(val);
                            hs.WriteByte(val);
                        }

                        Assert.AreEqual(len, ms.Position);
                        Hash expect = Hash.SHA256(ms.ToArray());
                        Hash actual = hs.Close();

                        Assert.AreEqual(expect, actual);
                        Assert.AreEqual(expect.ToArray(), actual.ToArray());
                        Assert.AreEqual(expect.ToString(), actual.ToString());

                        //wrapped test
                        actual = hsWrap.FinalizeHash();
                        Assert.AreEqual(expect, actual);
                        Assert.AreEqual(expect.ToArray(), actual.ToArray());
                        Assert.AreEqual(expect.ToString(), actual.ToString());
                    }
        }