You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

5000次SHA512哈希转换结果不符,请求排查问题

问题:SHA512迭代哈希结果与目标不符

我需要对字符串:

20191018143572123034102012221314181237774212

执行5000次SHA512哈希转换,预期结果为:

f8b915776eab735fdd10266b2e66068447904852b82c30eeb6de30703a087eb17ea4c4a37630494607194ddb9354c1211bd984fb5f4d9cff95f5a24ed52065e7

但我生成的最终哈希值为:

B4CA157DBBC9DFC22A8046092C00367CBDB51F0D780A428CB7731251FA29D1E426E0853E0F52A1ACA8460D6D1679477509490D7F4305DCDC7D77FD0CC1993D3A

我的代码如下:

static void Main(string[] args)
{
    string sha512 = "";
    string puvodni = "20191018143572123034102012221314181237774212";
    Console.WriteLine("Puvodni: " + puvodni);
    for (int i = 0; i < 5000; i++)
    {
        sha512 = TestCrypt.ComputeSHA512(puvodni);
        Console.WriteLine("Hash: " + sha512);
        puvodni=sha512;
    }        
}

internal class TestCrypt
{
    public static string ComputeSHA512(string s)
    {
        StringBuilder sb = new StringBuilder();
        using (SHA512 sha512 = SHA512.Create())
        {
            byte[] hashValue = sha512.ComputeHash(Encoding.UTF8.GetBytes(s));
            foreach (byte b in hashValue)
            {
                sb.Append($"{b:X2}");
            }
        }

        return sb.ToString();
    }
}

编辑补充:
我仍有问题,已更新程序,尝试使用内置SHA512函数及BouncyCastle库,但结果一致:

static void Main(string[] args)
{            
    string puvodni = "20191018143572123034102012221314181237774212";
    Console.WriteLine("Puvodni: " + puvodni);

    //varianta 1
    SHA512 sha512 = SHA512.Create();
    byte[] hash = Encoding.UTF8.GetBytes(puvodni);
    for (int i = 0; i < 2; i++)
    {
        hash=sha512.ComputeHash(hash);
        string result = BitConverter.ToString(hash).Replace("-", "");
        Console.WriteLine(i.ToString() + ". Hash: " + result);
    }

    //varianta 2
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(puvodni);
    Org.BouncyCastle.Crypto.Digests.Sha512Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha512Digest();
    for (int i = 0; i < 2; i++)
    {
        byte[] retValue = new byte[digester.GetDigestSize()];
        digester.BlockUpdate(bytes, 0, bytes.Length);
        digester.DoFinal(retValue, 0);
        string result1 = BitConverter.ToString(retValue).Replace("-", "");
        Console.WriteLine("BouncyCastle Hash: " + i.ToString() + ". " + result1);
        bytes=retValue;
    }      
}

我需要和某SHA512计算器页面结果一致,页面参数为:

  • NIP: 1435721230
  • 账号: 34 1020 1222 1314 1812 3777 4212
  • 日期: 18-10-2019
    首次迭代结果正常,但第二次迭代时我的程序生成的哈希值与页面不符。

问题原因与解决方案

核心问题

  1. 字符串中转错误:你当前的代码在迭代时,会把前一次的十六进制哈希字符串转成UTF-8字节再计算哈希,但目标页面的逻辑是直接用前一次哈希的原始字节数组作为下一次输入,不需要转成字符串,两者的输入字节内容完全不同。
  2. BouncyCastle Digest未重置:你的BouncyCastle实现中,循环复用了同一个Digest实例但未重置,导致后续哈希会累积之前的输入数据,计算逻辑错误。

修正后的代码

内置SHA512实现(正确迭代逻辑)

static void Main(string[] args)
{
    string initialStr = "20191018143572123034102012221314181237774212";
    Console.WriteLine("初始字符串: " + initialStr);

    using (SHA512 sha512 = SHA512.Create())
    {
        // 首次哈希:从初始字符串转字节计算
        byte[] currentHash = sha512.ComputeHash(Encoding.UTF8.GetBytes(initialStr));
        Console.WriteLine("1. Hash: " + BitConverter.ToString(currentHash).Replace("-", "").ToLower());

        // 后续4999次迭代:直接用前一次的哈希字节作为输入
        for (int i = 1; i < 5000; i++)
        {
            currentHash = sha512.ComputeHash(currentHash);
        }

        // 最终结果转小写,和预期格式统一
        string finalResult = BitConverter.ToString(currentHash).Replace("-", "").ToLower();
        Console.WriteLine("最终哈希: " + finalResult);
    }
}

BouncyCastle实现(修正重置问题)

static void Main(string[] args)
{
    string initialStr = "20191018143572123034102012221314181237774212";
    Console.WriteLine("初始字符串: " + initialStr);

    byte[] currentBytes = Encoding.UTF8.GetBytes(initialStr);
    
    for (int i = 0; i < 5000; i++)
    {
        // 每次迭代创建新的Digest实例(或调用digester.Reset()重置状态)
        Org.BouncyCastle.Crypto.Digests.Sha512Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha512Digest();
        byte[] retValue = new byte[digester.GetDigestSize()];
        
        digester.BlockUpdate(currentBytes, 0, currentBytes.Length);
        digester.DoFinal(retValue, 0);
        
        currentBytes = retValue;
        
        if (i == 0)
        {
            Console.WriteLine("1. BouncyCastle Hash: " + BitConverter.ToString(retValue).Replace("-", "").ToLower());
        }
    }

    string finalResult = BitConverter.ToString(currentBytes).Replace("-", "").ToLower();
    Console.WriteLine("BouncyCastle最终哈希: " + finalResult);
}

关键说明

  • 跳过字符串中转:直接传递哈希后的字节数组,避免十六进制字符串编码带来的字节内容变化,这是结果不一致的核心原因。
  • Digest状态重置:使用BouncyCastle时,每次迭代必须重置Digest状态,否则会累积之前的输入,导致哈希计算错误。
  • 大小写统一:预期结果是小写十六进制,转换时用ToLower()对齐格式。

内容的提问来源于stack exchange,提问作者Pavel Matras

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 08:30:46