代码修改前
const crypto = require("crypto"); const md5 = crypto.createHash("md5"); /**验签sign参数加密 * @param $data * @param $appSecret * @return string */ const makeSign = ($data, $appSecret) => { let $str = ""; let $index = 0; let $sortPor = []; for (let key in $data) { $sortPor.push(`${key}=${$data[key]}`); } // 排序 $sortPor.sort(); // 转url for (let key in $sortPor) { $str = `${$str}${$index === 0 ? "" : "&"}${$sortPor[key]}`; $index++; } // md5加密 const $ret = md5 .update(`${$str}&key=${$appSecret}`) .digest("hex"); return $ret; }; module.exports = makeSign;
代码修改后
const crypto = require("crypto"); // const md5 = crypto.createHash("md5"); /**验签sign参数加密 * @param $data * @param $appSecret * @return string */ const makeSign = ($data, $appSecret) => { let $str = ""; let $index = 0; let $sortPor = []; for (let key in $data) { $sortPor.push(`${key}=${$data[key]}`); } // 排序 $sortPor.sort(); // 转url for (let key in $sortPor) { $str = `${$str}${$index === 0 ? "" : "&"}${$sortPor[key]}`; $index++; } // md5加密 const $ret = crypto .createHash("md5") .update(`${$str}&key=${$appSecret}`) .digest("hex"); return $ret; }; module.exports = makeSign;
一个crypto实例只能调用digest一次,每次加密重新调用crypto