title: Migrate C# Hash Code to PHP
body: <p>I know there are similar questions already on SO but none of them seem to address this problem. I have inherited the following c# code that has been used to create password hashes in a legacy .net app, for various reasons the C# implementation is now being migrated to php:</p>
  
  <pre><code>string input = "fred";
  SHA256CryptoServiceProvider provider = new SHA256CryptoServiceProvider();
  byte[] hashedValue = provider.ComputeHash(Encoding.ASCII.GetBytes(input));
  string output = "";
  string asciiString = ASCIIEncoding.ASCII.GetString(hashedValue);
  foreach ( char c in asciiString ) {
     int tmp = c;
     output += String.Format("{0:x2}", 
               (uint)System.Convert.ToUInt32(tmp.ToString()));
  }
  return output;
  </code></pre>
  
  <p>My php code is very simple but for the same input "fred" doesn't produce the same result:</p>
  
  <pre><code>$output = hash('sha256', "fred");
  </code></pre>
  
  <p>I've traced the problem down to an encoding issue - if I change this line in the C# code:</p>
  
  <pre><code>string asciiString = ASCIIEncoding.ASCII.GetString(hashedValue);
  </code></pre>
  
  <p>to </p>
  
  <pre><code>string asciiString = ASCIIEncoding.UTF7.GetString(hashedValue);
  </code></pre>
  
  <p>Then the php and C# output match (it yields d0cfc2e5319b82cdc71a33873e826c93d7ee11363f8ac91c4fa3a2cfcd2286e5).</p>
  
  <p>Since I'm not able to change the .net code I need to work out how to replicate the results in php.</p>
  
  <p>Thanks in advance for any help,</p>