import loadjs from 'loadjs';

const RSAEcrypt = (openRSA, password) => {
    return new Promise((resolve, reject) => {
        //异步加载ras文件
        loadjs.isDefined('rsa') ? loadjs.ready('rsa', () => {
            if (openRSA && openRSA == '1') {
                password = __RSAEcrypt__.rsa_data_encrypt(password);
            }
            resolve(password);
        }) : loadjs(["/js/rsa/jsencrypt.js", "/js/rsa/rsa.js"], 'rsa', {
            async: false,
            success: () => {
                if (openRSA && openRSA == '1') {
                    const callback = () => {
                        password = __RSAEcrypt__.rsa_data_encrypt(password);
                        resolve(password);
                    }
                    __RSAEcrypt__.initRsaCode(callback);
                } else {
                    resolve(password);
                }
            }
        });
    })
}

export {
    RSAEcrypt
}