问题
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=
回答1:
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
回答2:
Hi your problem is in "YU" character. Your c# code looks fine. When you debug the code only with this character, you will get byte array with values 208 174 which is really Ю character in UTF8. So c# code should works fine and sha1 from c# should be good.
Can you get byte numbers from php only for char Ю? It should be also 208 and 174. If you remove this character your hash string should be equal.
来源:https://stackoverflow.com/questions/38867088/sha1-different-results-for-php-and-c-sharp