0

I'm using the following code to get the binance api signature, but it doesn't work!! can any one help!

int timeStamp = DateTime.now().millisecondsSinceEpoch;
String queryParams = 'timestamp=' + timeStamp.toString();

List<int> key = convert.utf8.encode(this.apiSecret);
crypto.Hmac hmac = new crypto.Hmac(crypto.sha256, key);
List<int> messageBytes = convert.utf8.encode(queryParams);
crypto.Digest digest = hmac.convert(messageBytes);
String signature = hex.encode(digest.bytes);
Shaban
  • 11
  • 4

2 Answers2

0

Update 16 Aug 2021

Binance has updated new Signature key format with SHA512 algorithm for more detail you can take a look at Binance api document.

String signature = timestamp.toString() + "\n" + nonce + "\n" + bytes + 
"\n";

YOU MUST INCLUDE "\n"

My Code

I have try Signature key that print from this code on Binance payment api, everything work fine. (signature must be uppercase when you send request)

var key = utf8.encode('YOUR_SECRET_KEY'); // get from binance api
var timestamp = DateTime.now().millisecondsSinceEpoch;
var nonce = "asdopewrmweqjidsfnosd"; // Random 32 length String,
var bytes = '''{
  "merchantId": "",
  "subMerchantId": "",
  "merchantTradeNo": "9825382937292117596", 
  "totalFee": 0.5,
  "productDetail": "Greentea ice cream cone",
  "currency": "USDT", 
  "returnUrl": "",
  "tradeType": "WEB", 
  "productType": "Food",
  "productName": "Ice Cream"
 }''';
String payload = timestamp.toString() + "\n" + nonce + "\n" + bytes + 
"\n";

var hmacSha512 = Hmac(sha512, key); // HMAC-SHA512/
var digest = hmacSha512.convert(utf8.encode(payload));

print(timestamp.toString() + "\n");
print("HMAC digest as hex string: ${digest.toString().toUpperCase()}");
0

You can use my new dart package: https://pub.dev/packages/binance_api_dart/versions/1.0.1 it is a http wrapper for binance api call. It is able to create the signature and return the http response.

Invictus
  • 426
  • 3
  • 13