0

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=
Stargazer
  • 478
  • 2
  • 9

2 Answers2

3

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

Bert
  • 2,318
  • 1
  • 12
  • 9
  • My problem was that I try to run PHP script from Windows console and pass "str" from console. That's why I get wrong hash. When I write the string inside the script it shows right result. – Stargazer Aug 10 '16 at 13:46
0

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.

MartinS
  • 751
  • 3
  • 12
  • 27