Files
td_official/src/utils/sign.js
2025-09-04 11:06:45 +08:00

51 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import crypto from 'crypto-js';
let coyr = 'coyrOrtiehanhan1223202409111457';
// 生成 nonce随机生成一个16位的字符串
function generateNonce() {
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let nonce = '';
for (let i = 0; i < 16; i++) {
nonce += chars.charAt(Math.floor(Math.random() * chars.length));
}
return nonce;
}
// 生成当前时间戳(格式为 yyyyMMddHHmmss
function generateTimestamp() {
return new Date().getTime();
}
// 生成 MD5 哈希(需要引入第三方库,如 crypto-js
function md5Hash(str) {
return crypto.MD5(str).toString();
}
// 生成 SHA-256 哈希(需要引入第三方库,如 crypto-js
function sha256Hash(str) {
return crypto.SHA256(str).toString();
}
// 生成签名
function generateSignature(params, timestamp, nonce) {
// 加密
let coyr1 = md5Hash(coyr);
// 拼接时间戳、nonce 和 secretKey
let signStr = coyr1 + timestamp + nonce;
// MD5 加密
const md5Str = md5Hash(signStr);
// SHA-256 加密
let aaaa = sha256Hash(md5Str);
return aaaa;
}
function sign(data) {
// 生成签名
const timestamp = generateTimestamp();
const nonce = generateNonce();
const sign = generateSignature(data, timestamp, nonce);
return {
timestamp,
nonce,
sign,
};
}
// 封装
export default sign;