// src/utils/encryption.js import sm2 from 'sm-crypto/src/sm2' import sm3 from 'sm-crypto/src/sm3' import sm4 from 'sm-crypto/src/sm4' // 示例密钥对,实际使用中需要从安全来源获取 const privateKey = 'your-private-key' const publicKey = 'your-public-key' // 生成随机盐值(16 字节) const salt = '2cc0c5f9f1749f1632efa9f63e902323' const sm4Key = 'your-sm4-key' // SM4 对称密钥,需要为 128 比特 (16 字节) // SM2 加密 export function encryptSM2(data) { // 使用公钥对数据进行加密 return sm2.doEncrypt(data, publicKey, 1) // 1 表示 C1C3C2 加密模式 } // SM2 解密 export function decryptSM2(data) { // 使用私钥对数据进行解密 return sm2.doDecrypt(data, privateKey, 1) // 1 表示 C1C3C2 加密模式 } // SM3 哈希 export function hashSM3(data) { // 对数据进行哈希计算 return sm3(data) } // 使用 SM3 进行哈希并加入盐值 export function hashWithSM3AndSalt(text) { // 将文本和盐值拼接在一起 const textWithSalt = salt + text // 使用 SM3 进行哈希 return hashSM3(textWithSalt) } // SM4 加密 export function encryptSM4(data) { // 使用对称密钥对数据进行加密 const sm4Instance = new sm4() return sm4Instance.encrypt(data, sm4Key) } // SM4 解密 export function decryptSM4(data) { // 使用对称密钥对数据进行解密 const sm4Instance = new sm4() return sm4Instance.decrypt(data, sm4Key) }