Kraken API - 签名无效

时间:2016-09-19 16:38:56

标签: java c# http-post

我正在尝试将一个工作示例从C sharp更改为Java,但我正在努力,不知道问题出在哪里。我联系了Kraken,他们建议我签名错误......回复是:

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

这是C sharp版本:

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

很抱歉发布了大量代码,任何帮助都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码中没有cchmac 512加密。这就是为什么你得到这个EAPI:无效的签名&#34;