const NodeRSA = require('node-rsa');
const fs = require('fs');
function makeRSAKeys(b = 1024) {
const rsa_keys = new NodeRSA({b: b});
try {
fs.statSync('./rsa_keys/public.key');
fs.statSync('./rsa_keys/private.key');
rsa_keys.importKey(fs.readFileSync('./rsa_keys/public.key', 'utf8'));
rsa_keys.importKey(fs.readFileSync('./rsa_keys/private.key', 'utf8'));
} catch (e) {
if (e.code === 'ENOENT') {
fs.writeFileSync('./rsa_keys/public.key', rsa_keys.exportKey('pkcs8-public-pem'));
fs.writeFileSync('./rsa_keys/private.key', rsa_keys.exportKey('pkcs8-private-pem'));
} else {
console.log(e);
}
}
}
makeRSAKeys();