本文整理汇总了C#中System.Security.Cryptography.HashAlgorithm.TransformFinalBlock方法的典型用法代码示例。如果您正苦于以下问题:C# HashAlgorithm.TransformFinalBlock方法的具体用法?C# HashAlgorithm.TransformFinalBlock怎么用?C# HashAlgorithm.TransformFinalBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了HashAlgorithm.TransformFinalBlock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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));
}
}