title: Generate a PHP UTF-16 SHA1 hash to match C# method
body: <p>I'm trying to replicate some C# code in PHP5 and am having some difficulties.</p>
  
  <p>The C# code is as following, and it is important to note that it cannot be changed:</p>
  
  <pre><code>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));
  </code></pre>
  
  <p>The PHP code I've written to replicate this is as follows:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>Please can any advise on some ideas?</p>
  
  <hr>
  
  <p>OK, I have altered the code following Artefacto's advice, and it still isn't working as expected.</p>
  
  <p>The PHP Code now looks like this:</p>
  
  <pre><code>$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."&lt;Br/&gt;";
  </code></pre>
  
  <p>And the outputed value of this method is:</p>
  
  <blockquote>
    <p>1/Y5MCzI8vDJqc456YIicpwoyy0=</p>
  </blockquote>
  
  <p>However, from the C# code, the value is this:</p>
  
  <blockquote>
    <p>VPf7BhT1ksAfWbzeJw35g+bVKwY=</p>
  </blockquote>