2013-10-31 13 views
7

はbitstampの新しい認証は、次の言葉:Bitstamp - C#で新しい認証 - 署名

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

出典:link

私は新しい署名を追加するために、次のコード(およびその他のパラメータを持っています):

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

そして、私はこのエラーを取得する:

{"error": "Invalid signature"}

誰でも問題がどのようなものかを知っていますか?私は自分のパラメータを100回チェックしたが、それは間違っていない。多分、誰かが新しい認証のために(C#で)動作するコードを手に入れたでしょうか?

UPDATE

AbhinavはStringToByteArray方法が間違っていた、正しかった(タイプミスだけでなく:P)作業コードです:

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

好奇心が強い、RESTにはどのようなスタックを使用しますか? – LamonteCristo

+1

@ makerofthings7私はRESTSharpを使用します。 – Julian

+0

@Julian私はbitstamp APIを理解するのに苦労しています。 http://stackoverflow.com/questions/21612185/restsharp-bitstamp-authentication-fails – Freddy

答えて

5

あなたは(間違っていたStrinToByteArraystr.ToCharArray()を使用しています同じシステムで使用されている場合にのみ修正してください)。 ASCIIエンコーディングなどを使用する必要があります。

+2

あなたは私のヒーローです!小さな変更(更新を参照)は、コードを機能させました。テストの後に気づいたASCIIの代わりにUTF8を使用することもできます。 – Julian