-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateKeypair.js
More file actions
39 lines (36 loc) · 1.27 KB
/
generateKeypair.js
File metadata and controls
39 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* This module will generate a public and private keypair and save to current directory
*
* Make sure to save the private key elsewhere after generated!
*/
const crypto = require("crypto");
const fs = require("fs");
function genKeyPair() {
// Generates an object where the keys are stored in properties `privateKey` and `publicKey`
const keyPair = crypto.generateKeyPairSync("rsa", {
modulusLength: 4096, // bits - standard for RSA keys
publicKeyEncoding: {
type: "pkcs1", // "Public Key Cryptography Standards 1"
format: "pem", // Most common formatting choice
},
privateKeyEncoding: {
type: "pkcs1", // "Public Key Cryptography Standards 1"
format: "pem", // Most common formatting choice
},
});
// Write the keypair also the current directory
fs.access(`./`, fs.F_OK, (err) => {
if (!err) {
if (process.argv.length > 2) {
const keyName = process.argv[2];
fs.writeFileSync(`./id_rsa_pub_${keyName}.pem`, keyPair.publicKey);
fs.writeFileSync(`./id_rsa_priv_${keyName}.pem`, keyPair.privateKey);
} else {
fs.writeFileSync(`./id_rsa_pub.pem`, keyPair.publicKey);
fs.writeFileSync(`./id_rsa_priv.pem`, keyPair.privateKey);
}
}
});
}
// Generate the keypair
genKeyPair();