public TransformBlock ( byte inputBuffer, int inputOffset, int inputCount, byte outputBuffer, int outputOffset ) : int | ||
inputBuffer | byte | |
inputOffset | int | |
inputCount | int | |
outputBuffer | byte | |
outputOffset | int | |
return | int |
using System; using System.Text; using System.Security.Cryptography; class Program { static void Main(string[] args) { byte[] input1 = Encoding.UTF8.GetBytes("hello"); byte[] input2 = Encoding.UTF8.GetBytes("world"); SHA256 sha256 = SHA256.Create(); // Transform first input byte[] hash1 = sha256.TransformBlock(input1, 0, input1.Length, null, 0); // Transform second input byte[] hash2 = sha256.TransformFinalBlock(input2, 0, input2.Length); // Combine hashes byte[] combinedHash = new byte[32]; Array.Copy(hash1, combinedHash, hash1.Length); Array.Copy(hash2, 0, combinedHash, hash1.Length, hash2.Length); Console.WriteLine(BitConverter.ToString(combinedHash).Replace("-", "")); } }
using System; using System.IO; using System.Security.Cryptography; class Program { static void Main(string[] args) { string filePath = @"C:\myfile.bin"; SHA256 sha256 = SHA256.Create(); byte[] buffer = new byte[8192]; using (FileStream fileStream = File.OpenRead(filePath)) { int bytesRead; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { sha256.TransformBlock(buffer, 0, bytesRead, null, 0); } } sha256.TransformFinalBlock(buffer, 0, 0); byte[] hash = sha256.Hash; Console.WriteLine(BitConverter.ToString(hash).Replace("-", "")); } }This code reads a large binary file in chunks and generates the SHA-256 hash code for it using the TransformBlock method. The TransformFinalBlock method is called after reading all chunks and finalizes the hash value. Both examples use the System.Security.Cryptography package library for C#.
public TransformBlock ( byte inputBuffer, int inputOffset, int inputCount, byte outputBuffer, int outputOffset ) : int | ||
inputBuffer | byte | |
inputOffset | int | |
inputCount | int | |
outputBuffer | byte | |
outputOffset | int | |
return | int |