Last active
May 9, 2021 01:16
-
-
Save flleeppyy/fb18d7c0db711ff5402e21963470dd25 to your computer and use it in GitHub Desktop.
Random RSA generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
namespace Express{ | |
interface Application { | |
rsa: { | |
passphrase: string; | |
privateKey?: string; | |
publicKey?: string; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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