-1

I try to convert a php-code to c#

php

$apikey='xxx';
$apisecretkey='yyy';
$nonce=time();
$uri='https://website.com/api/getsomething?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecretkey);
$resource = curl_init($uri);
curl_setopt($resource, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($resource);
$obj = ($execResult);

and here my C#

WebRequest _WebRequest;
WebResponse _WebResponse;
string apikey = "xxx";
string apisecret = "yyy";
string nonce = ((int)(DateTime.UtcNow - new DateTime(1970,1,1)).TotalSeconds).ToString();
string uri = $"https://website.com/api/getsomething?apikey={apikey}nonce={nonce}";
string _Hash_hmac = Hash_hmac(uri, apisecret);

_WebRequest = WebRequest.Create(uri);
_WebRequest.Headers["apisign"] = _Hash_hmac;

_WebResponse = _WebRequest.GetResponse();

The Code for Hash-hmac I use this C# equivalent to hash_hmac in PHP

The Webresponse is always "NONCE_NOT_PROVIDED"

I checked the outcome from the hash_hmac with the one in php - they are the same. And also try string ToUpper and ToLower.

What I am doing wrong?

  • replace the line **string uri = $"https://website.com/api/getsomething?apikey={apikey}nonce={nonce}";** with this line **string uri = $"https://website.com/api/getsomething?apikey={apikey}&nonce={nonce}";** – Asif Mehmood Nov 17 '17 at 02:30
  • I Thought the '&' it was a part of the PHP syntax. Now it looks better. Just get "INVALID_SIGNATURE" – Gustavo Besade Nov 17 '17 at 09:51
  • This might be due to the parameters you are passing. You should debug the code ( both **php** & **c#** ) because the date-value you're passing in c# is in different format. – Asif Mehmood Nov 17 '17 at 10:26
  • Problem solved. Had to change the HMACSHA256 to HMACSHA512 to calculate the hash. It is not written in the documentation of the API Thanks for the help – Gustavo Besade Nov 17 '17 at 10:39
  • Good luck @GustavoBesade – Asif Mehmood Nov 17 '17 at 10:55

1 Answers1

0

Aren't you missing an ampersand (&)?

apikey={apikey}nonce={nonce}

becomes

apikey={apikey}&nonce={nonce}

I would advise reading your code more closely with these kinds of errors.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86