title: Replicating C# SHA256 hash in PHP
body: <p>I am trying to access an API which requires header authorization in PHP. This authorization uses in part a SHA256 hash with a key. The API creators have supplied me with an .exe written in C# to create this hash. However, now it is not feasible to use the .exe and I would like to do it in PHP.</p>
  
  <p>Here is the correct C# code that makes the hash.</p>
  
  <pre><code>var url = "[url]";
  
  var userId = "apiuser";
  
  var timestamp = "Fri, 14 Jul 2017 00:28:07 GMT"; // DateTime.UtcNow;
  
  var keyString = "dGVzdA==";
  
  var hashData = String.Format("GET\n{0}\n{1}\n{2}\n", url, userId, timestamp); //.ToString("r"));
  
  var key = Convert.FromBase64String(keyString);
  string hashString;
  
  using (var hmac = new HMACSHA256(key))
  {
      var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(hashData));
      hashString = Convert.ToBase64String(hash);
  }
  Console.WriteLine(hashString);
  </code></pre>
  
  <p>C# Hash: CfS6zNR3pTP0kJIA0RJ7LUwQXjONroVIg65bDvuefH8=</p>
  
  <p>Here is my attempt to replicate it in PHP</p>
  
  <pre><code>$key = "dGVzdA==";
  $user = "apiuser";
  $url = "[url]";
  
  $timestamp = "Fri, 14 Jul 2017 00:28:07 GMT"; // date("D, d M Y H:i:s e");
  
  $hashdata = 'GET\n' . $url . '\n' . $user . '\n' . $timestamp;
  
  $generatedhash = base64_encode(hash_hmac('sha256', $hashdata, base64_decode($key), true));
  
  print_r($generatedhash);
  </code></pre>
  
  <p>PHP Hash: 6cI0NV6AKYILTyYHs+hyA0+Q4iRfMw+H2FGsp7uKOFM=</p>
  
  <p>I have attempted lots of different approaches to the PHP generated hash and none have been the same. Also I am not sure if the PHP date is the same as the C# date but that can wait. Any help is appreciated.</p>