Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HashAlgorithm.TransformFinalBlock Method

Computes the hash value for the specified region of the specified byte array.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

Public Function TransformFinalBlock ( _
	inputBuffer As Byte(), _
	inputOffset As Integer, _
	inputCount As Integer _
) As Byte()
Dim instance As HashAlgorithm
Dim inputBuffer As Byte()
Dim inputOffset As Integer
Dim inputCount As Integer
Dim returnValue As Byte()

returnValue = instance.TransformFinalBlock(inputBuffer, inputOffset, inputCount)
public final byte[] TransformFinalBlock (
	byte[] inputBuffer, 
	int inputOffset, 
	int inputCount
public final function TransformFinalBlock (
	inputBuffer : byte[], 
	inputOffset : int, 
	inputCount : int
) : byte[]
Not applicable.



The input to compute the hash code for.


The offset into the byte array from which to begin using data.


The number of bytes in the byte array to use as data.

Return Value

An array that is a copy of the part of the input that is hashed.

Exception typeCondition


inputCount uses an invalid value.


inputBuffer has an invalid offset length.


inputBuffer is a null reference (Nothing in Visual Basic).


inputOffset is out of range. This parameter cannot be a non-negative number.


The object has already been disposed.

You must call the TransformFinalBlock method after calling the TransformBlock method but before you retrieve the final hash value.

Note that the return value of this method is not the hash value, but only a copy of the hashed part of the input data. To retrieve the final hashed value after calling the TransformFinalBlock method, get the byte array contained in the Hash property.

The following code examples use the TransformFinalBlock method with the TransformBlock method to hash a string.

Imports System
Imports System.Text
Imports System.Security.Cryptography

Class Program

	Public Shared Sub Main()
		Dim rnd As RandomNumberGenerator = RandomNumberGenerator.Create
		Dim input() As Byte = New Byte((20) - 1) {}
		Console.WriteLine("Input        : {0}"& vbLf, BytesToStr(input))
		PrintHashMultiBlock(input, 1)
		PrintHashMultiBlock(input, 2)
		PrintHashMultiBlock(input, 3)
		PrintHashMultiBlock(input, 5)
		PrintHashMultiBlock(input, 10)
		PrintHashMultiBlock(input, 11)
		PrintHashMultiBlock(input, 19)
		PrintHashMultiBlock(input, 20)
		PrintHashMultiBlock(input, 21)
	End Sub

	Public Shared Function BytesToStr(ByVal bytes() As Byte) As String
		Dim str As StringBuilder = New StringBuilder
		Dim i As Integer = 0
		Do While (i < bytes.Length)
		str.AppendFormat("{0:X2}", bytes(i))
		i = (i + 1)
		Return str.ToString
	End Function

	Public Shared Sub PrintHash(ByVal input() As Byte)
		Dim sha As SHA256Managed = New SHA256Managed
		Console.WriteLine("ComputeHash  : {0}", BytesToStr(sha.ComputeHash(input)))
	End Sub

	Public Shared Sub PrintHashOneBlock(ByVal input() As Byte)
		Dim sha As SHA256Managed = New SHA256Managed
		sha.TransformFinalBlock(input, 0, input.Length)
		Console.WriteLine("FinalBlock   : {0}", BytesToStr(sha.Hash))
	End Sub

	Public Shared Sub PrintHashMultiBlock(ByVal input() As Byte, ByVal size As Integer)
		Dim sha As SHA256Managed = New SHA256Managed
		Dim offset As Integer = 0

		While ((input.Length - offset)  _
			>= size)
		offset = (offset + sha.TransformBlock(input, offset, size, input, offset))

		End While
		sha.TransformFinalBlock(input, offset, (input.Length - offset))
		Console.WriteLine("MultiBlock {0:00}: {1}", size, BytesToStr(sha.Hash))
	End Sub
End Class

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

© 2015 Microsoft