HashAlgorithm.TransformFinalBlock(Byte[], Int32, Int32) 方法


计算指定字节数组的指定区域的哈希值。Computes the hash value for the specified region of the specified byte array.

 virtual cli::array <System::Byte> ^ TransformFinalBlock(cli::array <System::Byte> ^ inputBuffer, int inputOffset, int inputCount);
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount);
abstract member TransformFinalBlock : byte[] * int * int -> byte[]
override this.TransformFinalBlock : byte[] * int * int -> byte[]
Public Function TransformFinalBlock (inputBuffer As Byte(), inputOffset As Integer, inputCount As Integer) As Byte()



要计算其哈希代码的输入。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.



一个数组,该数组是输入中计算了哈希值的部分的副本。An array that is a copy of the part of the input that is hashed.



inputCount 使用了无效值。inputCount uses an invalid value.

- 或 --or- inputBuffer 具有无效偏移量长度。inputBuffer has an invalid offset length.

inputBuffernullinputBuffer is null.

inputOffset 超出范围。inputOffset is out of range. 此参数需要非负数。This parameter requires a non-negative number.

对象已被释放。The object has already been disposed.


下面的代码示例将 TransformFinalBlock 方法与 TransformBlock 方法一起使用来对字符串进行哈希处理。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];

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

    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));


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


在调用 TransformBlock 方法后,但在检索最终哈希值之前,必须调用 TransformFinalBlock 方法。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. 若要在调用 TransformFinalBlock 方法后检索最终哈希值,请获取 Hash 属性中包含的字节数组。To retrieve the final hashed value after calling the TransformFinalBlock method, get the byte array contained in the Hash property.