// Generate a random string of the specified length
generateRandomStr() {
let code = '';
const chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
const charsArr = chars.split('');
for (let i = 0; i < 32; i++) {
const num = Math.floor(Math.random() * charsArr.length);
code += charsArr[num];
}
return code;
}encrypt-keyMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdHnzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ=="crypto-js": "^4.1.1","jsencrypt": "3.3.1"import JSEncrypt from 'jsencrypt';
import CryptoJS from 'crypto-js';
// Encrypt
export const encrypt = (txt: string) => {
const encryptor = new JSEncrypt();
encryptor.setPublicKey(publicKey); // Set public key
return encryptor.encrypt(txt); // Encrypt data
};
/**
* Base64 encoding
* @returns {string}
*/
export const encryptBase64 = (str: CryptoJS.lib.WordArray) => {
return CryptoJS.enc.Base64.stringify(str);
};
/**
* Generate a random AES key
* @returns {string}
*/
export const generateAesKey = () => {
return CryptoJS.enc.Utf8.parse(generateRandomStr());
};token, clientid, and encrypt-key, the API can be called.request.headers['Authorization'] = 'Bearer ' + getToken();
request.headers['clientid'] = clientid;
request.headers['encrypt-key'] = encrypt(encryptBase64(aesKey));
request.data = typeof request.data === 'object'
? encryptWithAes(JSON.stringify(request.data), aesKey)
: encryptWithAes(request.data, aesKey);