import CryptoJS from 'crypto-js';

const DEFAULT_KEY = 'SK'; // 加密密钥（口令，可以自己换）

/**
 * 加密
 */
export function encrypt(text: string, key: string = DEFAULT_KEY): string {
  return CryptoJS.AES.encrypt(text, key).toString();
}

/**
 * 解密
 */
export function decrypt(cipherText: string, key: string = DEFAULT_KEY): string {
  const bytes = CryptoJS.AES.decrypt(cipherText, key);
  return bytes.toString(CryptoJS.enc.Utf8);
}

const SK = 'U2FsdGVkX1/iQRVGm2kCZOyzkiAAXjP+65RuEZEOcK+p/j+8YoWrBjMbnSyQu2mL';

export function generateSign(params: { msgId: string; url: string; nonce: string }): string {
  const { msgId, url, nonce } = params;
  const newStr = [msgId, url, nonce].join('&');
  return CryptoJS.HmacSHA256(newStr, decrypt(SK)).toString(CryptoJS.enc.Hex);
}


