I'm trying to replicate some C# code in PHP5 and am having some difficulties.
The C# code is as following, and it is important to note that it cannot be changed:
string s = strToHash;
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes(s);
SHA1Managed managed = new SHA1Managed();
bytes = encoding.GetBytes(Convert.ToBase64String(managed.ComputeHash(bytes)) + "Space");
return Convert.ToBase64String(managed.ComputeHash(bytes));
The PHP code I've written to replicate this is as follows:
utfString = mb_convert_encoding($strToHash,"UTF-16");
hashTag = sha1($utfString,true);
base64Tag = base64_encode($hashTag);
encodedBase64Tag = mb_convert_encoding($base64Tag."Space","UTF-16");
base64EncodedAgain = base64_encode($encodedBase64Tag);
echo $base64EncodedAgain
However, the two outputs don't match up. I believe this is because the SHA1 method in PHP works on ASCII encoded strings, not the encoding actually used by the passed in string.
I would like to get this to work, and I can't achieve it by altering the c# code (although no-doubt that would be the easiest fix).
Please can any advise on some ideas?
OK, I have altered the code following Artefacto's advice, and it still isn't working as expected.
The PHP Code now looks like this:
$utfString = "\xFF\xFE".mb_convert_encoding($strToHash,"UTF-16LE");
$hashTag = sha1($utfString,true);
$base64Tag = base64_encode($hashTag);
$encodedBase64Tag = "\xFF\xFE".mb_convert_encoding($base64Tag."Space","UTF-16LE");
$hashedAgain = sha1($encodedBase64Tag,true);
$base64EncodedAgain = base64_encode($hashedAgain);
echo $base64EncodedAgain."<Br/>";
And the outputed value of this method is:
1/Y5MCzI8vDJqc456YIicpwoyy0=
However, from the C# code, the value is this:
VPf7BhT1ksAfWbzeJw35g+bVKwY=