title: Kraken API - Invalid signature
body: <p>I'm trying to change a working example from C sharp to Java but i'm struggling and don't know where the problem is. I contacted Kraken and they advised me that I have the wrong signature... The response is:</p>
  
  <pre><code>{"error":["EAPI:Invalid signature"]}
  </code></pre>
  
  <p>Here's the C sharp version:</p>
  
  <pre><code>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);
                      }
                  }
              }
          }
  
      }
  </code></pre>
  
  <p>The full code is here at:</p>
  
  <p><a href="https://bitbucket.org/arrivets/krakenapi/src/cff138b017c38efde2db1a080fb765790a6d04c8/KrakenClient/KrakenClient.cs?at=master&amp;fileviewer=file-view-default" rel="nofollow">https://bitbucket.org/arrivets/krakenapi/src/cff138b017c38efde2db1a080fb765790a6d04c8/KrakenClient/KrakenClient.cs?at=master&amp;fileviewer=file-view-default</a></p>
  
  <p>Here is my Java version:</p>
  
  <pre><code>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 &amp; 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&lt;NameValuePair&gt; nvps = new ArrayList&lt;&gt;();
      nvps.add(new BasicNameValuePair("nonce", String.valueOf(nonce)));
      httpPost.setEntity(new UrlEncodedFormEntity(nvps));
  
      // Client &amp; 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();
  }
  </code></pre>
  
  <p>I'm sorry to have posted a large bits of code, any help would be appreciated. Thank you!</p>