HashAlgorithm.TransformBlock(Byte[], Int32, Int32, Byte[], Int32) メソッド

定義

入力バイト配列の指定した領域のハッシュ値を計算し、入力バイト配列の指定した領域を出力バイト配列の指定した領域にコピーします。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.

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

ハッシュ コードを計算する対象の入力データ。The input to compute the hash code for.

inputOffset
Int32

入力バイト配列内のデータの使用開始位置を示すオフセット。The offset into the input byte array from which to begin using data.

inputCount
Int32

データとして使用する入力バイト配列内のバイト数。The number of bytes in the input byte array to use as data.

outputBuffer
Byte[]

ハッシュ コードの計算に使用される入力配列の一部のコピー。A copy of the part of the input array used to compute the hash code.

outputOffset
Int32

データの書き込みの開始位置を示す出力バイト配列内のオフセット。The offset into the output byte array from which to begin writing data.

戻り値

書き込むバイト数。The number of bytes written.

実装

例外

inputCount が無効な値を使用しています。inputCount uses an invalid value.

- または --or- inputBuffer の長さが無効です。inputBuffer has an invalid length.

inputBuffernullです。inputBuffer 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];
        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));
    }

}

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

注釈

メソッドを呼び出す前TransformBlockTransformFinalBlock 、メソッドを呼び出す必要があります。You must call the TransformBlock method before calling the TransformFinalBlock method. 最終的なハッシュ値を取得する前に、両方のメソッドを呼び出す必要があります。You must call both methods before you retrieve the final hash value.

メソッドのTransformFinalBlock呼び出し後に最終的なハッシュ値を取得するには、 Hashプロパティに格納されているバイト配列を取得します。To retrieve the final hash value after calling the TransformFinalBlock method, get the byte array contained within the Hash property.

異なる入力TransformBlock配列と出力配列を使用してメソッドをIOException呼び出すと、が発生します。Calling the TransformBlock method with different input and output arrays results in an IOException.

適用対象