This repository was archived by the owner on Jul 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeygen-es256.js
More file actions
61 lines (52 loc) · 1.67 KB
/
Copy pathkeygen-es256.js
File metadata and controls
61 lines (52 loc) · 1.67 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const { getArgs, askQuestion } = require('./codegen-util');
const write = (str, filePath, flags) => {
const writer = fs.createWriteStream(filePath, { flags });
writer.write(str);
writer.end();
};
const main = async () => {
console.log('Key generation started...');
try {
let { name } = getArgs();
// Check that the required flags are in
if (!name) {
console.log('File name is NOT found!');
name = await askQuestion('File name, plz? [es256] ');
if (name === '') name = 'es256';
}
// ES256 Key pair
const { publicKey, privateKey } = crypto.generateKeyPairSync('ec', {
namedCurve: 'P-256',
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
},
});
const prvfile = `${name}_prv.pem`;
const pubfile = `${name}_pub.pem`;
const prvFilePath = path.join(__dirname, 'keys', prvfile);
const pubFilePath = path.join(__dirname, 'keys', pubfile);
try {
await fs.promises.access(prvFilePath);
await fs.promises.access(pubFilePath);
console.log('Key pair files with the given name already exists');
const ans = await askQuestion('Do you want to overwrite it? [y/n] ');
if (ans !== 'y' && ans !== 'Y') process.exit(1);
} catch (error) {
//Do nothing
}
write(privateKey.toString(), prvFilePath, 'w');
write(publicKey.toString(), pubFilePath, 'w');
console.log(`Keys(${prvfile}, ${pubfile}) have been successfully created`);
} catch (error) {
console.log(error);
}
};
main();