title: Using a C# sha512 hash to communicate a php API
body: <p>I have a web app that want to use payment API that was developed with PHP. The php code for connecting the API works fine, and it is as below:</p>
  
  <pre><code>//set variables
  $api = 'https://voguepay.com/api/';
  $ref = time().mt_rand(0,999999);
  $task = 'pay'; 
  $merchant_id = '6619-0048444';
  $my_username = 'atah';
  $merchant_email_on_voguepay = 'omec1010@yahoo.com';
  $ref = time().mt_rand(0,9999999);
  $command_api_token = 'XPuz39v2RFzgdEUbPqcyTMg8HRkjGA';
  $hash = hash('sha512',$command_api_token.$task.$merchant_email_on_voguepay.$ref);
  
  $fields['task'] = $task;
  $fields['merchant'] = $merchant_id;
  $fields['ref'] = $ref;
  $fields['hash'] = $hash;
  $fields['amount'] = 20;
  $fields['seller'] = 'omec1010@gmail.com';
  $fields['memo'] = 'Payment '; 
  
  $fields_string = 'json='.urlencode(json_encode($fields));
  
  
  //open curl connection
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $api);
  curl_setopt($ch,CURLOPT_HEADER, false); //we dont need the headers
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// data coming back is put into a string
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
  curl_setopt($ch,CURLOPT_MAXREDIRS,2);
  $reply_from_voguepay = curl_exec($ch);//execute post
  curl_close($ch);//close connection
  </code></pre>
  
  <p>My C# code for connecting the API is below, but when I run the code I get error message the says <strong>"invalid Hash"</strong>. I feel that the hash produced by C# is different from that of PHP.
  Which other way can my code be make to get exact out of the PHP hash?</p>
  
  <pre><code>class Program
  {
      private const string task = "Pay";
  
      //Replace with your unique command api token
      private const string command_api_token = "XPuz39v2RFzgdEUbPqcyTMg8HRkjGA";
  
      //your registered email on voguepay
      private const string merchant_email_on_voguepay = "omec1010@yahoo.com";
  
  
  
      static void Main(string[] args)
      {
  
          Random rnd = new Random();
          string refl = string.Format("{0}{1}", (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds, new Random().Next(0, 9999999));
          byte[] hash_target = Encoding.Default.GetBytes(command_api_token + task + merchant_email_on_voguepay + refl);
          SHA512 sha512 = new SHA512CryptoServiceProvider();
  
          byte[] cryptString = sha512.ComputeHash(hash_target);
          string hashD = BitConverter.ToString(cryptString).Replace("-", String.Empty).ToUpper();
  
  
          //load all fields as json and serialize
          var keyValues = new Dictionary&lt;string, string&gt;
             {
                           { "task", "pay"},
                           { "merchant", "6619-0048444"},
                           { "ref",refl},
                           { "hash",hashD},
                           { "amount", "20"},
                           { "seller", "omec1010@gmail.com"},
                           { "remarks", "Payment"},
             };
  
          //serialization using Newtonsoft JSON 
          string json = JsonConvert.SerializeObject(keyValues);
  
          //url encode the json
          var postString = WebUtility.UrlEncode(json);
  
          //calling API with Restsharp
          var client = new RestClient("https://voguepay.com/api/");
          var request = new RestRequest(Method.POST);
          request.AddParameter("json", json);
          IRestResponse response = client.Execute(request);
  
          Console.WriteLine(response.Content);
          Console.ReadKey();
  
  
      }
  }
  </code></pre>
  
  <p>NB: The parameter values I supplied are real data, I set it up for testing purpose, never mind, you can use it to review my work.</p>