duanpenpan5796 2016-08-10 07:39
浏览 248
已采纳

SHA1:PHP和C#的结果不同

I have a code to generate hash in C#:

string hash = GetHash("Ю-41241624.05.1991");

public static string GetHash(string str)
{
    Encoding eu = Encoding.UTF8;
    byte[] data = eu.GetBytes(str);
    SHA1 sha = new SHA1CryptoServiceProvider();
    return Convert.ToBase64String(sha.ComputeHash(data)); 
}

The result is:

G7xY+gb35Lw4HlDnTZP89FU3Khk=

And I try to get the same result in PHP:

$str = mb_convert_encoding("Ю-41241624.05.1991","UTF-8");
$hash = sha1($str,true);       
$base64 = base64_encode($hash);  
echo $base64;

But the result is:

Dg+x7F8lsC/r9O8PNskgJ/MwNgU=
  • 写回答

2条回答 默认 最新

  • dsk49208 2016-08-10 10:37
    关注

    Just get rid of mb_convert_encoding(), if the string is already UTF-8 it will mess things up. When I run the code without that function I get the correct result: https://eval.in/620412

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?