Skip to content

Instantly share code, notes, and snippets.

@flleeppyy
Last active May 9, 2021 01:16
Show Gist options
  • Save flleeppyy/fb18d7c0db711ff5402e21963470dd25 to your computer and use it in GitHub Desktop.
Save flleeppyy/fb18d7c0db711ff5402e21963470dd25 to your computer and use it in GitHub Desktop.
Random RSA generation
declare global {
namespace Express{
interface Application {
rsa: {
passphrase: string;
privateKey?: string;
publicKey?: string;
}
}
}
}
app.rsa = {
passphrase: utils.randomString(32),
};
console.log("RSA Passphrase is " + app.rsa.passphrase);
if (fs.existsSync("./keys") === false) {
fs.mkdirSync("keys");
}
try { fs.rmSync("./keys/private.pem"); } catch (error) {}
try { fs.rmSync("./keys/public.pem"); } catch (error) {}
console.log("Generating key chain");
const privategen = childprocess.exec(`openssl genrsa -des3 -passout pass:${app.rsa.passphrase} -out keys/private.pem 2048`);
privategen.once("close", (error) => {
if (error) {
return reject("OpenSSL private generate command exited with error " + error);
}
const publicgen = childprocess.exec(`openssl rsa -in keys/private.pem -passin pass:${app.rsa.passphrase} -outform PEM -pubout -out keys/public.pem`);
publicgen.once("close", (error) => {
if (error) {
return reject("OpenSSL public generate command exited with error");
}
app.rsa.privateKey = fs.readFileSync("./keys/private.pem").toString();
app.rsa.publicKey = fs.readFileSync("./keys/public.pem").toString();
console.log("Done generating key chain");
resolve();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment