【问题标题】:PHP SHA-256 not the same as this c# code?PHP SHA-256 和这个 c# 代码不一样?
【发布时间】:2014-02-22 06:44:52
【问题描述】:

遇到问题了——也许……看来这一段PHP:

$mydigestb = hash("sha256",$digeststring);

不会生成与此 C# 代码相同的结果:

private string GenerateDigest(long currentTime)
{
    SHA256Managed hashString = new SHA256Managed();
    StringBuilder hex = new StringBuilder();
    byte[] hashValue = hashString.ComputeHash(Encoding.UTF8.GetBytes(String.Format("{0}{1}", currentTime, txtApiKey.Text)));
    foreach (byte x in hashValue)
    {
        hex.AppendFormat("{0:x2}", x);
    }
    return hex.ToString();
}

输入的值是一样的,但是出来的好像不一样。

【问题讨论】:

  • 它们对我来说是一样的。显示您正在测试它的确切值。也许是字符编码差异?

标签: c# php hash sha


【解决方案1】:

对我来说也是这样:

PHP 代码:http://codepad.org/gcGC5Omp

<?php
$mydigestb = hash("sha256" , "abhinav" );
echo $mydigestb;
?>

C#代码:https://ideone.com/jrz05O

using System;

public class Test
{
    public static void Main()
    {
        System.Security.Cryptography.SHA256Managed hm = new System.Security.Cryptography.SHA256Managed();
        byte[] hashValue = hm.ComputeHash(System.Text.Encoding.ASCII.GetBytes("abhinav"));
        Console.WriteLine(System.BitConverter.ToString(hashValue).Replace("-", "").ToLower());
    }
}

您是否使用相同的样本数据?

【讨论】:

  • 你救了我。我错误地转换回字符串,您的 sn-p 向我展示了方法。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 2020-12-06
  • 1970-01-01
  • 2014-08-19
  • 2011-05-18
相关资源
最近更新 更多