const querystring = require('querystring');
const uuid = require('uuid');
const appkey = '';
const secretkey = '';
const timestamp = Math.floor(Date.now() / 1000);
const params = {
appkey: appkey,
timestamp: timestamp,
nonce: timestamp,
};
// 步骤4:按照参数名进行字典序排序
const sortedParams = Object.keys(params).sort().reduce((result, key) => {
result[key] = params[key];
return result;
}, {});
// 步骤5:拼接参数字符串
const queryString = querystring.stringify(sortedParams);
// 步骤6:HMAC-SHA256加密
var hash = CryptoJS.HmacSHA256(queryString, secretkey);
var hmac = CryptoJS.enc.Base64.stringify(hash);
// 步骤7:MD5加密并截取
let signatureStr = CryptoJS.MD5(hmac).toString();
console.log(signatureStr)
let signature = signatureStr.substr(5, 10);
console.log(queryString,signature)
// 步骤8:将signature加入参数中
const finalParams = Object.assign({}, sortedParams, { signature: signature });
// 构造请求参数
const finalQueryString = querystring.stringify(finalParams);
console.log(finalQueryString);
网友评论