Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Bittrex API in Java Android: INVALID_SIGNATURE

wilkas:

I am trying to use Bittrex Api in AsyncTask. I get an error INVALID_SIGNATURE, so I assume that there is something wrong with HMAC SHA512 encryption or its HEX conversion. Can you provide with any lead how to address this issue?

PHP code to access Bittrex API is this (http://ift.tt/2r0THRG):


$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='http://ift.tt/1z78eXQ'.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

My Java code. I think that the issue is either in HMAC SHA512 encryption or HEX conversion:


protected String doInBackground(String... strings) {

String api_key = strings[0];
String api_secret = "My secret";

try {

String nonce = String.valueOf(System.currentTimeMillis() / 1000L);

URL url = new URL("http://ift.tt/2iKISBe"+api_key+"&nonce="+nonce);

String sign = hash_hmac(url, api_secret);

Log.i("MyTag", "Signature: "+sign);

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("apisign", sign);

try {

InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}

bufferedReader.close();
inputStreamReader.close();

return stringBuilder.toString();

} finally {
urlConnection.disconnect();
}


} catch (Exception e) {
e.printStackTrace();

return null;
}

}

private String hash_hmac(URL url, String api_secret) {

Mac sha512_HMAC;
String result;

try {
byte [] byteKey = api_secret.getBytes("UTF-8");
final String HMAC_SHA512 = "HmacSHA512";
sha512_HMAC = Mac.getInstance(HMAC_SHA512);
SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
sha512_HMAC.init(keySpec);
byte [] mac_data = sha512_HMAC.
doFinal(url.toString().getBytes("UTF-8"));
result = bytesToHex(mac_data);
return result;
} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
return null;
}

}

public static String bytesToHex(byte[] bytes) {
final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Bittrex API in Java Android: INVALID_SIGNATURE

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×