30 lines
1.2 KiB
JavaScript
30 lines
1.2 KiB
JavaScript
import { Encrypt, Crypto } from "./crypto";
|
|
export const encryptConfig = async (config) => {
|
|
let secretKey = randomRange(32, 32);
|
|
let encryptSecretKey = await Encrypt.encrypt(secretKey); // 密钥进行非对称加密
|
|
if (encryptSecretKey) {
|
|
config.headers['X-Encrypted-Key'] = encryptSecretKey;
|
|
}
|
|
if (config.data && Object.prototype.toString.call(config.data) === '[object Object]') {
|
|
Object.keys(config.data).forEach(async key => {
|
|
config.data[key] = Crypto.AES.encrypt(config.data[key], secretKey)
|
|
console.log(config.data[key], 'KEY')
|
|
// config.data[key] = await Encrypt.encrypt(config.data[key])
|
|
})
|
|
}
|
|
return config;
|
|
}
|
|
const randomRange = (min, max, charStr) => {
|
|
var returnStr = "",
|
|
range;
|
|
if (typeof max == 'string') {
|
|
charStr = max;
|
|
}
|
|
range = ((max && typeof max == 'number') ? Math.round(Math.random() * (max - min)) + min : min);
|
|
charStr = charStr || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
for (var i = 0; i < range; i++) {
|
|
var index = Math.round(Math.random() * (charStr.length - 1));
|
|
returnStr += charStr.substring(index, index + 1);
|
|
}
|
|
return returnStr;
|
|
} |