0

I have a code which looks like this, It's basically a JSON request which I need to pass to the server and code in PHP looks like this:

<?php


$client_secret= '';
$data= array(

'email' => '**********',

'password' => '******',

'client_id' => '*******'
);

$api_url='******';

$json_data=json_encode($data);

$signature_string = md5($json_data . $client_secret); 


$post_data = 'signature='.$signature_string.'&data='.urlencode($json_data);

$curl = curl_init($api_url); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_POST, 1); 

curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

$result = curl_exec($curl); 

print_r($result);


curl_close($curl); 

?> 

And this works fine in PHP. So now I'm trying to translatet this code into C#.

What I did so far is:

   var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        string client_secret = "*****";

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"email\":\"******\"," +
                          "\"password\":\"******\"}" +
                          "\"client_id\":\"******\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        string resp = string.Empty;
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            resp = streamReader.ReadToEnd();
        }
        return resp;

Basically I now have my data in JSON format. What now I need to do is replicate the PHP's md5 function to create a MD5 hash out of my client_secret string + JSON string which was created in previous step and then simply post the data string to server.

Can someone help me out with this ?

User987
  • 3,663
  • 15
  • 54
  • 115
  • Googling C# md5 gives [this](https://msdn.microsoft.com/en-us/library/system.security.cryptography.md5(v=vs.110).aspx) – Jacob Lambert Mar 18 '17 at 19:04
  • @JRLambert the C#'s md5 option receives only 1 parameter as input.. How do I actually pass both my client_secret and jsondata to get a same signature ? – User987 Mar 18 '17 at 19:08
  • You are still only passing one argument to the `md5` function in PHP. The `.` operator concats the strings together. So for the C# equivalent, use `+`. – Jacob Lambert Mar 18 '17 at 20:28

0 Answers0