CSharpTest.Net
HashStream Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace : HashStream Class

Glossary Item Box

Represents a writtable stream for computing the hash value without retaining the data

Syntax

Visual Basic (Declaration) 
Public NotInheritable Class HashStream 
   Inherits CSharpTest.Net.IO.AggregateStream
C# 
public sealed class HashStream : CSharpTest.Net.IO.AggregateStream 

Example

Library/Library.Test/TestHash.cs

C#Copy Code
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());
}
VB.NETCopy Code
Dim r As New Random()
Dim test As Byte()() = New Byte()() {New Byte(300) {}, New Byte(1) {}, New Byte(500) {}, New Byte(11) {}, New Byte(1) {}, New Byte(1000) {}}
Using hs As New HashStream(New SHA256Managed())
    Using ms As New MemoryStream()
        Using hsWrap As New HashStream(New SHA256Managed(), ms)
            Assert.IsTrue(hs.CanWrite)
            Dim len As Long = 0
            For Each bytes As Byte() In test
                len += bytes.Length
                r.NextBytes(bytes)
                hsWrap.Write(bytes, 0, bytes.Length)
                hs.Write(bytes, 0, bytes.Length)
            Next
            Dim i As Integer = 0
            While i < 5
                len += 1
                Dim val As Byte = DirectCast(r.[Next](255), Byte)
                hsWrap.WriteByte(val)
                hs.WriteByte(val)
                System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            End While

            Assert.AreEqual(len, ms.Position)
            Dim expect As Hash = Hash.SHA256(ms.ToArray())
            Dim actual As Hash = 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())
        End Using
    End Using
End Using

Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.IO.Stream
         CSharpTest.Net.IO.AggregateStream
            CSharpTest.Net.Crypto.HashStream

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys