Export (0) Print
Expand All

HashAlgorithm.TransformBlock Method

Updated: August 2010

Computes the hash value for the specified region of the input byte array and copies the specified region of the input byte array to the specified region of the output byte array.

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

public int TransformBlock(
	byte[] inputBuffer,
	int inputOffset,
	int inputCount,
	byte[] outputBuffer,
	int outputOffset
)

Parameters

inputBuffer
Type: System.Byte[]

The input to compute the hash code for.

inputOffset
Type: System.Int32

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

inputCount
Type: System.Int32

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

outputBuffer
Type: System.Byte[]

A copy of the part of the input array used to compute the hash code.

outputOffset
Type: System.Int32

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

Return Value

Type: System.Int32
The number of bytes written.

Implements

ICryptoTransform.TransformBlock(Byte[], Int32, Int32, Byte[], Int32)

ExceptionCondition
ArgumentException

inputCount uses an invalid value.

-or-

inputBuffer has an invalid length.

ArgumentNullException

inputBuffer is null.

ArgumentOutOfRangeException

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

ObjectDisposedException

The object has already been disposed.

You must call the TransformBlock method before calling the TransformFinalBlock method. You must call both methods before you retrieve the final hash value.

To retrieve the final hash value after calling the TransformFinalBlock method, get the byte array contained within the Hash property.

Calling the TransformBlock method with different input and output arrays results in an IOException.

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

using System;
using System.Security.Cryptography;
using System.Text;

class MainClass
{
	public static void Main()
	{
		RandomNumberGenerator rnd = RandomNumberGenerator.Create();

		byte[] input = new byte[20];
		rnd.GetBytes(input);

		Console.WriteLine("Input        : {0}\n", BytesToStr(input));
		PrintHash(input);
		PrintHashOneBlock(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);
	}

	public static string BytesToStr(byte[] bytes)
	{
		StringBuilder str = new StringBuilder();

		for (int i = 0; i < bytes.Length; i++)
			str.AppendFormat("{0:X2}", bytes[i]);

		return str.ToString();
	}

	public static void PrintHash(byte[] input)
	{
		SHA256Managed sha = new SHA256Managed();
		Console.WriteLine("ComputeHash  : {0}", BytesToStr(sha.ComputeHash(input)));
	}

	public static void PrintHashOneBlock(byte[] input)
	{
		SHA256Managed sha = new SHA256Managed();
		sha.TransformFinalBlock(input, 0, input.Length);
		Console.WriteLine("FinalBlock   : {0}", BytesToStr(sha.Hash));
	}

	public static void PrintHashMultiBlock(byte[] input, int size)
	{
		SHA256Managed sha = new SHA256Managed();
		int offset = 0;

		while (input.Length - offset >= size)
			offset += sha.TransformBlock(input, offset, size, input, offset);

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

}

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

Date

History

Reason

August 2010

Corrected method summary.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft