2016-09-19 53 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:无效签名“