You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
57 lines
1.6 KiB
const JSEncrypt = require('./jsencrypt.min.js');
|
|
import CryptoJS from "crypto-js";
|
|
|
|
|
|
// 支付密码加密
|
|
// 公钥
|
|
const keyPub = "-----BEGIN PUBLIC KEY-----\n" +
|
|
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1yLtKwRwC/Uv35920h7X4on8/\n"+
|
|
"hR8AuYnWOsyDCGbepY+1cMhSX2zOu8BUINk5UcwIbm62Ki7xZIWT9+1Wn0y7dfJb\n"+
|
|
"kub5O6/lM3oHuY7eT9alYs5kw8EWsznT/zeE9sapTJyPVDhRuFwslEayzPhx6lVR\n"+
|
|
"6lwfQTK/gfUtz8gbxwIDAQAB\n"+
|
|
"-----END PUBLIC KEY-----\n"
|
|
|
|
const handleEncrypt = (str) => {
|
|
const jsencrypt = new JSEncrypt();
|
|
jsencrypt.setPublicKey(keyPub); // 设置公钥
|
|
return jsencrypt.encrypt(str); // 对数据进行加密
|
|
}
|
|
|
|
// 生成支付加密参数
|
|
const getPWSignature = (password,nonce) => {
|
|
const salt = generateRandomSalt(16)
|
|
// 使用 PBKDF2 派生密钥
|
|
const iterations = 10000;
|
|
const keySize = 256 / 32; // 256 位密钥
|
|
const clientKey = CryptoJS.PBKDF2(password, salt, {
|
|
keySize: keySize,
|
|
iterations: iterations
|
|
}).toString(CryptoJS.enc.Base64);
|
|
// 生成 signature
|
|
const signature = CryptoJS.HmacSHA256(nonce, clientKey).toString(CryptoJS.enc.Base64)
|
|
|
|
return {
|
|
// nonce: nonce,
|
|
signature: signature,
|
|
client_salt: salt,
|
|
// clientKey: clientKey,
|
|
}
|
|
}
|
|
|
|
// 生成指定长度的随机 Salt
|
|
const generateRandomSalt = (length)=> {
|
|
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
let salt = '';
|
|
for (let i = 0; i < length; i++) {
|
|
const randomIndex = Math.floor(Math.random() * charset.length);
|
|
salt += charset.charAt(randomIndex);
|
|
}
|
|
return salt;
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
handleEncrypt: handleEncrypt,
|
|
getPWSignature: getPWSignature
|
|
}
|
|
|