2016-09-19 84 views
2

我正在嘗試將一個正在運行的示例從C sharp更改爲Java,但我很掙扎,不知道問題出在哪裏。我接觸的海妖,他們告訴我,我有錯誤的簽名...的反應是:Kraken API - 無效簽名

{"error":["EAPI:Invalid signature"]} 

這裏的升C版本:

private JsonObject QueryPrivate(string a_sMethod, string props = null) 
    { 
     // generate a 64 bit nonce using a timestamp at tick resolution 
     Int64 nonce = DateTime.Now.Ticks; 
     props = "nonce=" + nonce + props; 


     string path = string.Format("/{0}/private/{1}", _version, a_sMethod); 
     string address = _url + path; 
     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address); 
     webRequest.ContentType = "application/x-www-form-urlencoded"; 
     webRequest.Method = "POST"; 
     webRequest.Headers.Add("API-Key", _key); 


     byte[] base64DecodedSecred = Convert.FromBase64String(_secret); 

     var np = nonce + Convert.ToChar(0) + props; 

     var pathBytes = Encoding.UTF8.GetBytes(path); 
     var hash256Bytes = sha256_hash(np); 
     var z = new byte[pathBytes.Count() + hash256Bytes.Count()]; 
     pathBytes.CopyTo(z, 0); 
     hash256Bytes.CopyTo(z, pathBytes.Count()); 

     var signature = getHash(base64DecodedSecred, z); 

     webRequest.Headers.Add("API-Sign", Convert.ToBase64String(signature)); 

     if (props != null) 
     { 

      using (var writer = new StreamWriter(webRequest.GetRequestStream())) 
      { 
       writer.Write(props); 
      } 
     } 

     //Make the request 
     try 
     { 

      using (WebResponse webResponse = webRequest.GetResponse()) 
      { 
       using (Stream str = webResponse.GetResponseStream()) 
       { 
        using (StreamReader sr = new StreamReader(str)) 
        { 
         return (JsonObject)JsonConvert.Import(sr); 
        } 
       } 
      } 
     } 

    } 

完整的代碼是在這裏:

https://bitbucket.org/arrivets/krakenapi/src/cff138b017c38efde2db1a080fb765790a6d04c8/KrakenClient/KrakenClient.cs?at=master&fileviewer=file-view-default

這裏是我的Java版本:

private void fetch() throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException { 

    String version = "0"; 
    String key = ".....6"; 
    String secret = "....g=="; 
    long nonce = System.currentTimeMillis(); 

    String props = null; 
    props = "nonce=" + nonce + props; // I've tried this with and without the 'null' on the end 

    // url 
    String url = "https://api.kraken.com"; 
    String path = "/" + version + "/private/" + "Balance"; 
    String address = url + path; 

    // post req 
    HttpPost httpPost = new HttpPost(address); 

    // headers 
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 
    httpPost.setHeader("API-Key", key); 

    // decode buffer 
    BASE64Decoder decoder = new BASE64Decoder(); 
    byte[] base64DecodedSecred = decoder.decodeBuffer(secret); 

    // nonce & props 
    String np = nonce + (char) 0 + props; 

    // create byte array 
    byte[] pathBytes = path.getBytes("UTF-8"); 
    byte[] hash256Bytes = sha256(np); 
    byte[] z = new byte[pathBytes.length + hash256Bytes.length]; 
    System.arraycopy(pathBytes, 0, z, 0, pathBytes.length); 
    System.arraycopy(hash256Bytes, 0, z, pathBytes.length, hash256Bytes.length); 

    // encrypt signature 
    byte[] signature = hmacEncrypt(z, base64DecodedSecred); // my hmacEncrypt is message, secret (opposite to the c sharp) 
    BASE64Encoder encoder = new BASE64Encoder(); 
    httpPost.setHeader("API-Sign", encoder.encode(signature)); 

    // Post 
    List<NameValuePair> nvps = new ArrayList<>(); 
    nvps.add(new BasicNameValuePair("nonce", String.valueOf(nonce))); 
    httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 

    // Client & Response 
    CloseableHttpClient httpClient = HttpClients.createDefault(); 
    CloseableHttpResponse response = httpClient.execute(httpPost); 

    HttpEntity entity = response.getEntity(); 

    // parse 
    JsonParser jp = new JsonParser(); 
    JsonElement root = jp.parse(EntityUtils.toString(entity)); 
    System.out.println(root); // {"error":["EAPI:Invalid signature"]} 

    // close client 
    httpClient.close(); 
} 

我很抱歉發佈了大量的代碼,任何幫助將不勝感激。謝謝!

回答

0

代碼中沒有cchmac 512加密。這就是爲什麼你得到這個EAPI:無效簽名「