共用方式為


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

定義

計算輸入位元組陣列中指定區域的雜湊值,並將指定的輸入位元組陣列區域複製到指定的輸出位元組陣列區域。

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

參數

inputBuffer
Byte[]

要用來計算雜湊碼的輸入。

inputOffset
Int32

輸入位元組陣列中的座標,從此處開始使用資料。

inputCount
Int32

輸入位元組陣列中要用作資料的位元組數目。

outputBuffer
Byte[]

用來計算雜湊碼的輸入陣列部分之複本。

outputOffset
Int32

輸出位元組陣列中的座標,從此處開始寫入資料。

傳回

已寫入的位元組數目。

實作

例外狀況

inputCount 使用無效的值。

-或-

inputBuffer的長度無效。

inputBuffernull

inputOffset 超出範圍。 這個參數需要非負數的數字。

物件已經處置。

範例

下列程式代碼範例使用 TransformFinalBlock 方法搭配 TransformBlock 方法來哈希字串。

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));
    }
}
/*
This example produces output similar to the following:

Input        : 45D97219908A572DE336B9DEC787C311D3349F69

ComputeHash  : 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
FinalBlock   : 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 01: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 02: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 03: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 05: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 10: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 11: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 19: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 20: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
MultiBlock 21: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15

*/

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) {}
        rnd.GetBytes(input)
        Console.WriteLine("Input        : {0}"& vbLf, 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)
    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)
        Loop
        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

' This example produces output similar to the following:
'
' Input        : 45D97219908A572DE336B9DEC787C311D3349F69
'
' ComputeHash  : 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' FinalBlock   : 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 01: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 02: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 03: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 05: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 10: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 11: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 19: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 20: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15
' MultiBlock 21: 84E87322C7E3BCA848B0A698EE66E9A9725DA786F9FD4FFD46C385F59AB35B15

備註

您必須先呼叫 方法, TransformBlock 才能呼叫 TransformFinalBlock 方法。 您必須先呼叫這兩種方法,才能擷取最終哈希值。

若要在呼叫 TransformFinalBlock 方法之後擷取最終哈希值,請取得 屬性中包含的 Hash 位元組陣列。

TransformBlock呼叫具有不同輸入和輸出陣列的方法會產生 IOException

適用於