42 lines
1.2 KiB
Dart
42 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
/// أداة توليد توقيع HMAC-SHA256 للمصادقة مع الباك إند
|
|
class HmacUtility {
|
|
/// Generate HMAC-SHA256 signature
|
|
/// message = timestamp|api_key|payload
|
|
static String generateSignature({
|
|
required String apiKey,
|
|
required String apiSecret,
|
|
required String payload,
|
|
required int timestamp,
|
|
}) {
|
|
final message = '$timestamp|$apiKey|$payload';
|
|
final hmac = Hmac(sha256, utf8.encode(apiSecret));
|
|
final digest = hmac.convert(utf8.encode(message));
|
|
return digest.toString();
|
|
}
|
|
|
|
/// Build authenticated headers for API requests
|
|
static Map<String, String> buildHeaders({
|
|
required String apiKey,
|
|
required String apiSecret,
|
|
required String payload,
|
|
}) {
|
|
final timestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
|
final signature = generateSignature(
|
|
apiKey: apiKey,
|
|
apiSecret: apiSecret,
|
|
payload: payload,
|
|
timestamp: timestamp,
|
|
);
|
|
|
|
return {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': apiKey,
|
|
'X-Signature': signature,
|
|
'X-Timestamp': timestamp.toString(),
|
|
};
|
|
}
|
|
}
|