2022-02-27 02:07:39 +00:00
|
|
|
import * as crypto from 'node:crypto';
|
|
|
|
import * as util from 'node:util';
|
2020-05-10 09:42:31 +00:00
|
|
|
|
|
|
|
const generateKeyPair = util.promisify(crypto.generateKeyPair);
|
|
|
|
|
|
|
|
export async function genRsaKeyPair(modulusLength = 2048) {
|
|
|
|
return await generateKeyPair('rsa', {
|
|
|
|
modulusLength,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
2021-12-09 14:58:30 +00:00
|
|
|
format: 'pem',
|
2020-05-10 09:42:31 +00:00
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: undefined,
|
2021-12-09 14:58:30 +00:00
|
|
|
passphrase: undefined,
|
|
|
|
},
|
2020-05-10 09:42:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function genEcKeyPair(namedCurve: 'prime256v1' | 'secp384r1' | 'secp521r1' | 'curve25519' = 'prime256v1') {
|
|
|
|
return await generateKeyPair('ec', {
|
|
|
|
namedCurve,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
2021-12-09 14:58:30 +00:00
|
|
|
format: 'pem',
|
2020-05-10 09:42:31 +00:00
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: undefined,
|
2021-12-09 14:58:30 +00:00
|
|
|
passphrase: undefined,
|
|
|
|
},
|
2020-05-10 09:42:31 +00:00
|
|
|
});
|
|
|
|
}
|