Bitstamp - new authentication in C # - signature

The new bitstamp authentication says the following:

The signature is an HMAC-SHA256 encoded message containing: nonce, a client identifier, and an API key. The HMAC-SHA256 code must be generated using the secret key that was generated by your API key. This code must be converted to a hexadecimal representation (64 uppercase characters). Example (Python): message = nonce + client_id + api_key signature = hmac.new (API_SECRET, msg = message, digestmod = hashlib.sha256) .hexdigest (). upper ()

Source: Link

I have the following code to add a new signature (and other parameters):

public void AddApiAuthentication(RestRequest restRequest) { var nonce = DateTime.Now.Ticks; var signature = GetSignature(nonce, apiKey, apiSecret, clientId); restRequest.AddParameter("key", apiKey); restRequest.AddParameter("signature", signature); restRequest.AddParameter("nonce", nonce); } private string GetSignature(long nonce, string key, string secret, string clientId) { string msg = string.Format("{0}{1}{2}", nonce, clientId, key); return ByteArrayToString(SignHMACSHA256(secret, StrinToByteArray(msg))).ToUpper(); } public static byte[] SignHMACSHA256(String key, byte[] data) { HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key)); return hashMaker.ComputeHash(data); } public static byte[] StrinToByteArray(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } public static string ByteArrayToString(byte[] hash) { return BitConverter.ToString(hash).Replace("-", "").ToLower(); } 

And then I get this error:

{"error": "Invalid signature"}

Has anyone understood what the problem is? I checked my parameters 100 times, and it is not. Maybe someone has a working piece of code (in C #) for new authentication?

UPDATE

Abhinav was right, the StringToByteArray method was wrong (not just a typo: P), working code:

 public static byte[] StrinToByteArray(string str) { return System.Text.Encoding.ASCII.GetBytes(str); } 
+7
security c # web-services hash bitcoin
source share
1 answer

You are using str.ToCharArray() in a StrinToByteArray , which is incorrect (ONLY when used on the same system). You need to use ASCII encoding or something like that.

+5
source share

All Articles