/** * 魔改的 encryptlong 版本, 只支持 encryptLong 方法 * 小程序上 JSEncrypt 为空不支持 @important */ import JSEncrypt from './encryptlong' import { deepClone } from './index' /** * 生成RSA公钥加密后的数据 * @param param0.publicKey RSA公钥 * @param param0.payload 加密前的数据 * @returns {string} 加密后的数据 */ export const getEncryptInfo = ({ publicKey = '', payload = {} as any } = {}) => { if (!publicKey) return '' try { const params = deepClone(payload) // 生成RSA实例 const rsaInstance = new JSEncrypt() // 设置公钥 rsaInstance.setPublicKey(publicKey) // 生成时间戳 // params.timestamp = +new Date() // 确定签名算法 // const signMethod = 'HmacSHA256' // 生成随机数 // const nonce = WordArray.random(16).toString() // 生成签名:基本参数、时间戳 + 随机数 // const signature = HmacSHA256(JSON.stringify(params), nonce).toString() // 将签名放入参数中 // params.signature = signature // params.nonce = nonce // params.signMethod = signMethod // rsa公钥加密 const encrypted = rsaInstance.encryptLong(typeof params === 'object' ? JSON.stringify(params) : params) return encrypted } catch (error) { console.error('encrypt error:', error) } return '' }