PHP SHA-256与这个c#代码不一样?

时间:2014-02-21 22:20:58

标签: c# php hash sha

遇到了问题 - 也许......看来这片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();
}

输入值是相同的,但看来出来的是不同的。

1 个答案:

答案 0 :(得分: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());
    }
}

您使用相同的样本数据吗?