可以粘到代码里面直接用
<script src="./js/core.js"></script>
<script src="./js/enc-base64.js"></script>
<script src="./js/md5.js"></script>
<script src="./js/evpkdf.js"></script>
<script src="./js/sha1.js"></script>
<script src="./js/hmac.js"></script>
<script src="./js/cipher-core.js"></script>
<script src="./js/aes.js"></script>
<script src="./js/pad-pkcs7.js"></script>
<script src="./js/mode-ecb.js"></script>
<script src="./js/enc-utf8.js"></script>
<script src="./js/enc-hex.js"></script>
function requestSignature (
appSecret,
accept,
params,
body = null,
httpMethod,
uriPath,
baseURL,
date,
codedHeaders,
time
) {
try {
let CanonicalizedHeaders = "";
if (typeof codedHeaders === "object") {
Object.keys(codedHeaders).map((key) => {
CanonicalizedHeaders += key + ":" + codedHeaders[key] + "\n";
});
} else {S
console.warn("参与加密headers对象为空");
}
if (uriPath.indexOf("http") === -1 && baseURL) {
// 无http头,使用了baseURL与短path, 拼接成完整url
uriPath = baseURL + uriPath;
}
// 参数处理
if (!params && uriPath.indexOf("?") === -1) {
// params无参, url内无拼接参数 params置为''
params = "";
// console.log('0)
} else if (uriPath.indexOf("?") > -1) {
// params无参, url内有拼接参数
let paramsArr = uriPath.split("?");
paramsArr = paramsArr.splice(1, paramsArr.length - 1);
let paramsStr = paramsArr.join("?"); //分割后再拼接,防止参数内包含?
let aParams = paramsStr.split("&");
if (!params) {
params = {};
}
for (let i = 0; i < aParams.length; i++) {
let aParam = aParams[i].split("=");
params[aParam[0]] = aParam[1];
}
// 排序
// params = this.jsonSort(params);
const paramsArray = [];
//参数拼接
Object.keys(params).forEach((key) =>
paramsArray.push(`${key}=${params[key]}`)
);
params = `${paramsArray.join("&")}`;
}
// 截取path
let domain = uriPath.split("/");
console.log('domain',domain)
// domain = domain.splice(3, domain.length - 3);
console.log('domain1',domain)
uriPath = "/" + domain.join("/");
if (uriPath.indexOf("?") > -1) {
uriPath = uriPath.split("?")[0];
}
console.log('uriPath',uriPath)
// console.log('params',params)
const CanonicalizedParameters = encodeURI(params);
console.log('CanonicalizedParameters',CanonicalizedParameters)
console.log('CanonicalizedHeaders',CanonicalizedHeaders)
const ContentMD5 = body? CryptoJS.enc.Base64.stringify(CryptoJS.MD5(JSON.stringify(body))) : '1B2M2Y8AsgTpgAmY7PhCfg==';
httpMethod = httpMethod.toUpperCase();
const stringSign =
accept +
"\n" +
CanonicalizedHeaders +
"\n" +
CanonicalizedParameters +
"\n" +
ContentMD5 +
"\n" +
time +
"\n" +
httpMethod +
"\n" +
uriPath;
// console.log('生成验签',stringSign)
return CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(stringSign, appSecret));
} catch (e) {
console.log("加签报错: ", e);
}
};
function random (min, max) {
// 若max不存在 min 赋值给max,并重新赋值min
if (max == null) {
max = min;
min = 0;
}
return min + Math.floor(Math.random() * (max - min + 1));
};
使用方法:
var requestSign = requestSignature(
"a9e12104f18040e39e6a30b3f12736d1",
"application/json;responseformat=3",
'',
null,
"POST",
'control/judgeCode?groupInviteCode=' + teamCommend,
// 'control/judgeCode',
"",
null,
{
"x-api-signature-nonce": signOnce,
"x-api-signature-version": "1.0",
},
Time
)