美文网首页
Postman 验签请求

Postman 验签请求

作者: Super淳语 | 来源:发表于2023-03-08 14:31 被阅读0次
    
    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);
    
    

    相关文章

      网友评论

          本文标题:Postman 验签请求

          本文链接:https://www.haomeiwen.com/subject/jcloldtx.html