Skip to content

Instantly share code, notes, and snippets.

@JonathanVeg
Created August 28, 2017 18:38
Show Gist options
  • Save JonathanVeg/ad64b6b7de75ed3492ab25f651cf4fd7 to your computer and use it in GitHub Desktop.
Save JonathanVeg/ad64b6b7de75ed3492ab25f651cf4fd7 to your computer and use it in GitHub Desktop.
// example for signed operation with Bittrex V1.1 API
// kotlin code
fun getBalances(key: String, secret: String, listener: Response.Listener<String>) {
var url = "https://bittrex.com/api/v1.1/account/getbalances?apikey=API_KEY&nonce=NONCE"
val ir = InternetRequests()
url = url.replace("API_KEY".toRegex(), key)
url = url.replace("NONCE".toRegex(), Utils.getNonce()) // 1
ir.addHeader("apisign", InternetRequests.hmacDigest(url, secret)) // 2
ir.executePost(url, listener)
}
// 1
fun getNonce(): String = (System.currentTimeMillis() / 1000).toString()
// 2
// hmacDigest is basically when you sign the message using the "HmacSHA512" as algo and the secret as key
// the secret key is used ONLY for signing the request. So, it does not have to be sent in requests.
private static String hmacDigest(String msg, String keyString, String algo) {
String digest = null;
try {
SecretKeySpec key = new SecretKeySpec(
(keyString).getBytes("UTF-8"), algo);
Mac mac = Mac.getInstance(algo);
mac.init(key);
byte[] bytes = mac.doFinal(msg.getBytes("ASCII"));
StringBuilder hash = new StringBuilder();
for (byte aByte : bytes) {
String hex = Integer.toHexString(0xFF & aByte);
if (hex.length() == 1)
hash.append('0');
hash.append(hex);
}
digest = hash.toString();
} catch (Exception e) {
e.printStackTrace();
}
return digest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment