I have this function in JS, using Forge library, for key pair generation. The problem is that when trying to use the async function to perform the generation, the function is returning before generating the result.
function generateKeys(keySize, storageName) {
var rsa = forge.pki.rsa;
// var keys = forge.pki.rsa.generateKeyPair(keySize);
var p1 = rsa.generateKeyPair({bits: keySize, workers: -1}, function(err, keypair) {
var privateKey = keypair.privateKey;
var publicKey = keypair.publicKey;
sessionStorage.setItem(storageName, JSON.stringify(privateKey));
var csr = forge.pki.createCertificationRequest();
csr.publicKey = publicKey;
csr.sign(privateKey);
// convert certification request to PEM-format
var certReqPem = forge.pki.certificationRequestToPem(csr);
console.log(certReqPem);
return certReqPem;
});
};
I get this return in my HTML to forward to a PHP form, however, it is coming NULL, but if I send printar, I see that it sends Null, and then printa in the console