Skip to content

Instantly share code, notes, and snippets.

@prdn
Created October 19, 2020 17:00
Show Gist options
  • Save prdn/bb3d40b08d2589406cb72c8e659c3f2d to your computer and use it in GitHub Desktop.
Save prdn/bb3d40b08d2589406cb72c8e659c3f2d to your computer and use it in GitHub Desktop.
bfx-auth-rest-v2.java
import java.util.Date;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
public final class Program {
static final String baseUrl = "https://api.bitfinex.com";
static final String apiKey = "";
static final String apiSecret = "";
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient();
String nonce = Long.toString((new Date().getTime()) * 1000l);
String apiPath = "v2/auth/r/info/user";
String body = "{}";
String signatureRaw = String.format("/api/%s%s%s", apiPath, nonce, body);
String signature = computeSignature(signatureRaw, apiSecret);
RequestBody reqBody = RequestBody.create(MediaType.parse("application/json"), "{}");
Request req = new Request.Builder().url(baseUrl + "/" + apiPath)
.addHeader("bfx-nonce", nonce)
.addHeader("bfx-apikey", apiKey)
.addHeader("bfx-signature", signature)
.post(reqBody).build();
Response res = client.newCall(req).execute();
String result = res.body().string();
System.out.println(result);
}
private static String computeSignature(String input, String secret) throws Exception {
SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA384");
final Mac mac = Mac.getInstance("HmacSHA384");
mac.init(signingKey);
byte[] digest = mac.doFinal(input.getBytes());
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1)
sb.append('0');
sb.append(hex);
}
return sb.toString();
}
@farwqmdhsnly
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment