实验报告 - C# HashAlgorithm.TransformBlock 方法

实验一

class HashAlgorithmTest
    {
        public static void Main()
        {
            byte[] data = Encoding.UTF8.GetBytes("abcdefghijklmn");

            HashAlgorithm sha1 = HashAlgorithm.Create("sha1");
            byte[] output = new byte[64];

            for (int i = 0; i < data.Length; i += 4)
            {
                var length = Math.Min(4, data.Length - i);
                sha1.TransformBlock(data, i, length, output, i);
                Console.WriteLine(BitConverter.ToString(output));
            }

            sha1.TransformFinalBlock(data, data.Length, 0);

            Console.WriteLine(BitConverter.ToString(sha1.Hash).Replace("-", string.Empty));

clipboard.png

clipboard.png

clipboard.png

clipboard.png

clipboard.png

实验二 官方示例

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

}

clipboard.png

clipboard.png

输出都是一样的。

clipboard.png

计算结果是定长的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值