Ok, so I am trying to generate a public and private key using crypto (https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback)
The thing is, one of the parameters of the generateKeyPair is a callback function, and I can't get my code to wait for the callback to run. It runs eventually, but by then I already tried to use the data. The idea is to do something like this:
const _getKeyPair = (pwd: string): Object => {
const { generateKeyPair }: any = require('crypto');
const keyPair = { publicKey: '', privateKey: '' };
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: pwd
}
}, (err: Error, publicKey: string, privateKey: string) => {
if (err) {
throw err;
}
keyPair.publicKey = publicKey;
keyPair.privateKey = privateKey;
});
return keyPair;
};
And call:
const keyPair = _getKeyPair('myPassword');