Run generateRSAPrivateKey() and generateCSR() in sync way
muklah opened this issue · comments
I'm trying to separate these two function, so I can call generateRSAPrivateKey()
that generate Key, then use this key as parameter in generateCSR()
openssl.generateRSAPrivateKey(keyOptions, (err, key, cmd) => {
fs.writeFile( `filename.key`, error => {
if (err) {
return console.log(error);
}
}
);
// Generate the CSR and save it into file
openssl.generateCSR(csroptions, key, "test", (error, csr, cmD) => {
if (error) {
console.log(error);
} else {
console.log(cmD.files.config);
}
fs.writeFile( `filename.csr`, csr, e => {
if (e) {
return console.log(e);
}
console.log("The csrInfo was saved!");
}
);
});
});
So, I want to can call then in order like this:
const key = openssl.generateRSAPrivateKey()
openssl.generateCSR(key)
I tried this solution but not working:
let key1 = '';
openssl.generateRSAPrivateKey(keyOptions, (err,key)=>{
fs.writeFile( `filename.key`,key);
key1 = key ;
}
openssl.generateCSR(csroption, key1,(err,csr)=>{
fs.writeFile( `filename.csr`,csr);
} )
)
Here key1 will be empty string
It might be helpful to research the use of Promises in Node.JS.
Thanks for the answer,I tried it in promises way and I passed callback function as a parameter,
but the question still Can I Run It in Sync Way?, in other words is there possible way to return the key as a string or buffer and assign it to a variable?
What you want to do is definitely possible, but I don't have enough context to be much help. I would make a variable with enough scope to use the private key wherever you need it and assign its value from within the callback of generateRSAPrivateKey. You just need to make sure that is done before you use the private key elsewhere. I'm not sure what you're using to "trigger" the creation of the CSR, but it can't happen until generateRSAPrivateKey calls it's callback and assigns your private key variable.
Thanks man I solve it by using callback and it works perfectly
Thanks again