CSharpTest.Net
SHA512(Stream) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Crypto Namespace > Hash Class > SHA512 Method : SHA512(Stream) Method

bytes

Glossary Item Box

Computes an SHA512 hash

Syntax

Visual Basic (Declaration) 
Public Overloads Shared Function SHA512( _
   ByVal bytes As Stream _
) As Hash
C# 
public static Hash SHA512( 
   Stream bytes
)

Parameters

bytes

Example

Library/Library.Test/TestIOStreamUtils.cs

C#Copy Code
byte[] testData = new byte[ushort.MaxValue];
new Random().NextBytes(testData);
Array.Clear(testData, 0, testData.Length / 2);
Hash compressedHash;
Hash hash = Hash.SHA512(testData);

using (TempFile f = new TempFile())
{
    f.WriteAllBytes(testData);
    Assert.AreEqual((long)testData.Length, f.Length);

    IOStream.Compress(f.TempPath);
    Assert.IsTrue(f.Length < testData.Length);
    compressedHash = Hash.SHA512(f.Read());
    Assert.IsFalse(compressedHash == hash);

    IOStream.Decompress(f.TempPath);
    Assert.AreEqual((long)testData.Length, f.Length);
    Assert.IsTrue(hash == Hash.SHA512(f.Read()));
}

MemoryStream ms = new MemoryStream(testData), gz = new MemoryStream();
IOStream.Compress(ms, gz);

Assert.IsTrue(compressedHash == Hash.SHA512(gz.ToArray()));
ms = new MemoryStream();
gz.Position = 0;
IOStream.Decompress(gz, ms);

Assert.IsTrue(hash == Hash.SHA512(ms.ToArray()));
VB.NETCopy Code
Dim testData As Byte() = New Byte(UShort.MaxValue) {}
New Random().NextBytes(testData)
Array.Clear(testData, 0, testData.Length / 2)
Dim compressedHash As Hash
Dim hash As Hash = Hash.SHA512(testData)

Using f As New TempFile()
    f.WriteAllBytes(testData)
    Assert.AreEqual(DirectCast(testData.Length, Long), f.Length)

    IOStream.Compress(f.TempPath)
    Assert.IsTrue(f.Length < testData.Length)
    compressedHash = Hash.SHA512(f.Read())
    Assert.IsFalse(compressedHash = hash)

    IOStream.Decompress(f.TempPath)
    Assert.AreEqual(DirectCast(testData.Length, Long), f.Length)
    Assert.IsTrue(hash = Hash.SHA512(f.Read()))
End Using

Dim ms As New MemoryStream(testData), gz As New MemoryStream()
IOStream.Compress(ms, gz)

Assert.IsTrue(compressedHash = Hash.SHA512(gz.ToArray()))
ms = New MemoryStream()
gz.Position = 0
IOStream.Decompress(gz, ms)

Assert.IsTrue(hash = Hash.SHA512(ms.ToArray()))

Requirements

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

See Also

Generated with Document! X 2011 by Innovasys