【问题标题】:Why isn't my PHP SHA256 hash equivalent to C# SHA256Managed hash为什么我的 PHP SHA256 哈希不等于 C# SHA256Managed 哈希
【发布时间】:2020-02-16 23:15:11
【问题描述】:

为什么这些不一样?

php:

    $hash = hash('sha256', $userData['salt'] . hash('sha256', $password) );

c#

    public static string ComputeHash(string plainText, string salt)
    {
        // Convert plain text into a byte array.
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);

        SHA256Managed hash = new SHA256Managed();

        // Compute hash value of salt.
        byte[] plainHash = hash.ComputeHash(plainTextBytes);

        byte[] concat = new byte[plainHash.Length + saltBytes.Length];

        System.Buffer.BlockCopy(saltBytes, 0, concat, 0, saltBytes.Length);
        System.Buffer.BlockCopy(plainHash, 0, concat, saltBytes.Length, plainHash.Length);

        byte[] tHashBytes = hash.ComputeHash(concat);

        // Convert result into a base64-encoded string.
        string hashValue = Convert.ToBase64String(tHashBytes);

        // Return the result.
        return hashValue;
    }

【问题讨论】:

  • 因为这个问题有缺陷。它是等价的。只是它看起来不一样
  • 可能是这样,但我添加了 base64_encode() 并且它们仍然不会产生相同的输出。

标签: c# php sha256


【解决方案1】:

C# 正在输出一个 base64 编码的字符串,而 PHP 正在输出一个十六进制的数字。更好的比较可能是将参数 true 传递给 PHP 的哈希函数的末尾和 base64 的结果:

 $hash = base64_encode(
           hash('sha256', $userData['salt'] . hash('sha256', $password), true )
         );

【讨论】:

  • 感谢您的快速回复!我试过了,尽管它们现在更接近了,但它们不会产生相同的输出。还有其他想法吗?
  • @Lemontongs 你对上面的代码做了什么修改吗...我在 php 和 c# 中得到了不同的结果
【解决方案2】:

因为它们不同。您的 C# 代码最后以 Base64 编码对计算的哈希进行编码。 PHP 只返回一串十六进制数字。

【讨论】:

    【解决方案3】:

    第一个嫌疑人:

    Encoding.UTF8.GetBytes(plainText);
    

    C# 使用 UTF-8,您的 PHP 可能不使用,但如果您严格使用来自 US-ASCII 子集的字母,您可能会很幸运。

    第二个嫌疑人:

    Convert.ToBase64String(tHashBytes);
    

    您的 PHP 中没有关于 Base64 的内容。

    由于 PHP 会给你一个十六进制编码的结果,你也应该在你的 C# 中切换到十六进制。解决方案见this answer

    【讨论】:

      【解决方案4】:

      好吧,我不是 C# 程序员,但让我印象深刻的一件事是:

      // Convert result into a base64-encoded string.
      string hashValue = Convert.ToBase64String(tHashBytes);
      

      您是否在 C# 中对最终输出进行 base64 编码?因为你不在 PHP 中...

      【讨论】:

        【解决方案5】:
        C#
        string toHash = "abcdefg";
        SHA256Managed hash = new SHA256Managed();
        byte[] signatureData = hash.ComputeHash(new UnicodeEncoding().GetBytes(toHash));
        string hashResult = System.Convert.ToBase64String(signatureData);
        PHP
        print base64_encode(hash("sha256",mb_convert_encoding("abcdefg","UTF-16LE"),true));
        

        像顶级代码一样写,它们是一样的

        【讨论】:

          【解决方案6】:

          C#

          string toHash = "abcdefg"; 
          SHA256Managed hash = new SHA256Managed(); 
          byte[] signatureData = hash.ComputeHash(new UnicodeEncoding().GetBytes(toHash)); 
          string hashResult = System.Convert.ToBase64String(signatureData); 
          

          PHP

          print base64_encode(hash("sha256",mb_convert_encoding("abcdefg","UTF-16LE"),true));
          

          为我工作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-12-08
            • 2018-12-03
            • 2012-02-09
            • 2021-06-21
            • 2015-07-27
            • 2015-10-25
            • 2011-06-08
            相关资源
            最近更新 更多