2013-10-31 16 views
7

La nuova autenticazione bitstamp dice quanto segue:Bitstamp - nuova autenticazione in C# - firma

Signature is a HMAC-SHA256 encoded message containing: nonce, client ID and API key. The HMAC-SHA256 code must be generated using a secret key that was generated with your API key. This code must be converted to it's 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()

Fonte: link

Ho il seguente codice per aggiungere la nuova firma (e di altri parametri):

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(); 
    } 

E poi ottengo questo errore:

{"error": "Invalid signature"}

Qualcuno ha un'idea di quale potrebbe essere il problema? Ho controllato i miei parametri 100 volte e quelli non sono sbagliati. Forse qualcuno ha ottenuto un pezzo di codice funzionante (in C#) per la nuova autenticazione?

UPDATE

Abhinav aveva ragione, il metodo StringToByteArray era sbagliato (non solo l'errore di battitura: P) il codice di lavoro è:

public static byte[] StrinToByteArray(string str) 
    { 
     return System.Text.Encoding.ASCII.GetBytes(str); 
    } 
+0

Curioso, quale stack usi per REST? – LamonteCristo

+1

@ makerofthings7 Io uso RESTSharp. – Julian

+0

@ Julian Sto attraversando un periodo difficile per capire l'API bitstamp, potresti aiutarmi? http://stackoverflow.com/questions/21612185/restsharp-bitstamp-authentication-fails – Freddy

risposta

5

Si utilizza str.ToCharArray() in StrinToByteArray che non è corretto (correggere SOLO quando utilizzato sullo stesso sistema). Devi usare la codifica ASCII o qualcosa del genere.

+2

Sei il mio eroe! Il piccolo cambiamento (vedi aggiornamento) ha fatto funzionare il codice. Potrebbe anche usare UTF8 invece di ASCII che ho notato dopo i test. – Julian