Skip to main content

RSA

RSA Signature Demo Code

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;

public class BydoxeRsaExample {

private static final String API_KEY = "your-api-key";
private static final String PASSPHRASE = "your-passphrase";
private static final String BASE_URL = "https://open-api.bydoxe.com";

// Your RSA private key (PKCS#8 format, without header/footer)
private static final String PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n MIIEvgIBADANBgkqhkiG9w0BAQEFAASC...your-private-key...-----END PRIVATE KEY-----";

/**
* Generate RSA SHA256 signature
* Message format: timestamp + method + requestPath + body
* If queryString exists: timestamp + method + requestPath + "?" + queryString + body
*/
public static String generateSignature(String privateKeyStr, String timestamp,
String method, String requestPath,
String queryString, String body) {
try {
String message;
if (queryString != null && !queryString.isEmpty()) {
message = timestamp + method.toUpperCase() + requestPath + "?" + queryString + body;
} else {
message = timestamp + method.toUpperCase() + requestPath + body;
}

byte[] keyBytes = Base64.getDecoder().decode(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(message.getBytes("UTF-8"));

byte[] signatureBytes = signature.sign();
return Base64.getEncoder().encodeToString(signatureBytes);
} catch (Exception e) {
throw new RuntimeException("Failed to generate RSA signature", e);
}
}

public static void main(String[] args) throws Exception {
// GET request example (without query string)
String timestamp = String.valueOf(System.currentTimeMillis());
String method = "GET";
String requestPath = "/api/v1/spot/account/assets";
String queryString = "";
String body = "";

String signature = generateSignature(PRIVATE_KEY, timestamp, method, requestPath, queryString, body);

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + requestPath))
.header("ACCESS-KEY", API_KEY)
.header("ACCESS-SIGN", signature)
.header("ACCESS-TIMESTAMP", timestamp)
.header("ACCESS-PASSPHRASE", PASSPHRASE)
.header("Content-Type", "application/json")
.GET()
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

// GET request example (with query string)
String timestamp2 = String.valueOf(System.currentTimeMillis());
String requestPath2 = "/api/v1/spot/market/tickers";
String queryString2 = "symbol=BTCUSDT";

String signature2 = generateSignature(PRIVATE_KEY, timestamp2, "GET", requestPath2, queryString2, "");

HttpRequest request2 = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + requestPath2 + "?" + queryString2))
.header("ACCESS-KEY", API_KEY)
.header("ACCESS-SIGN", signature2)
.header("ACCESS-TIMESTAMP", timestamp2)
.header("ACCESS-PASSPHRASE", PASSPHRASE)
.header("Content-Type", "application/json")
.GET()
.build();

HttpResponse<String> response2 = client.send(request2, HttpResponse.BodyHandlers.ofString());
System.out.println(response2.body());
}
}