Qbit签名

Qbit 发起的每个请求都包含一个sign参数,可用于验证来自 Qbit 的请求的真实性。对于每个请求,取出data参数的数据并通过 HMAC-SHA256 哈希函数对其进行处理。

签名流程

签名步骤:

  1. 待签名参数集合
const params = {
    'createTime': '2023-05-31T07:29:46.784Z',
    'budgetId': null,
    'provider': 'PrepaidCard_493728',
    'currency': 'USD',
    'qbitCardNoLastFour': '1234',
    'id': 'b9ce056b-c1f8-4f19-b014-d7be02a54598',
    'status': 'Active',
    'useType': '79f22263-a3fe-4347-8a40-2af6bf422839',
    'label': 'ce08100b-fca8-4a13-bbfc-c381aeaec5d0',
    'balanceId': 'ab43462f-93b3-4540-8601-11d759948ee7',
    'cardAddress': {
        'country': 'US',
        'postalCode': '94402',
        'addressLine2': '',
        'addressLine1': '20 Barneson ave',
        'state': 'California',
        'city': 'San Mateo'
    },
    'accountId': '01eba490-5f9c-48a6-aa2d-7bcfdff0d720',
    'token': '0ef85b24-866f-4c03-a7e8-459e3742642b',
    'userName': 'test test'
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  1. 将待签名参数集合key依据“字符串首位字符的ASCII码”进行升序排列(排序过程中若出现ASCII码值相同的情况,则依次递增对下一位进行比较)
const keys = Object.keys(params);
keys.sort();
1
2
  1. 拼接字符串, 空值以空字符串填充
accountId=01eba490-5f9c-48a6-aa2d-7bcfdff0d720&balanceId=ab43462f-93b3-4540-8601-11d759948ee7&budgetId=&cardAddress={"addressLine1":"20 Barneson ave","addressLine2":"","city":"San Mateo","country":"US","postalCode":"94402","state":"California"}&createTime=2023-05-31T07:29:46.784Z&currency=USD&id=b9ce056b-c1f8-4f19-b014-d7be02a54598&label=ce08100b-fca8-4a13-bbfc-c381aeaec5d0&provider=PrepaidCard_493728&qbitCardNoLastFour=1234&status=Active&token=0ef85b24-866f-4c03-a7e8-459e3742642b&useType=79f22263-a3fe-4347-8a40-2af6bf422839&userName=test test
1
  1. 用CLIENT_SECRET对拼接后的字符串做hmac-sha256签名,且以16进制编码,得到signature:8287d5539c03918c9de51176162c2bf7065d5a8756b014e3293be1920c20d102
const hmac = crypto.createHmac('sha256', '25d55ad283aa400af464c76d713c07ad');
const sign = hmac.update('拼接后的字符串').digest('hex');
1
2

示例代码