2023-01-12 21:40:33 -07:00
|
|
|
import * as crypto from "node:crypto";
|
|
|
|
import * as util from "node:util";
|
2020-05-10 03:42:31 -06:00
|
|
|
|
|
|
|
const generateKeyPair = util.promisify(crypto.generateKeyPair);
|
|
|
|
|
|
|
|
export async function genRsaKeyPair(modulusLength = 2048) {
|
2023-01-12 21:40:33 -07:00
|
|
|
return await generateKeyPair("rsa", {
|
2020-05-10 03:42:31 -06:00
|
|
|
modulusLength,
|
|
|
|
publicKeyEncoding: {
|
2023-01-12 21:40:33 -07:00
|
|
|
type: "spki",
|
|
|
|
format: "pem",
|
2020-05-10 03:42:31 -06:00
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
2023-01-12 21:40:33 -07:00
|
|
|
type: "pkcs8",
|
|
|
|
format: "pem",
|
2020-05-10 03:42:31 -06:00
|
|
|
cipher: undefined,
|
2021-12-09 07:58:30 -07:00
|
|
|
passphrase: undefined,
|
|
|
|
},
|
2020-05-10 03:42:31 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function genEcKeyPair(
|
|
|
|
namedCurve:
|
|
|
|
| "prime256v1"
|
|
|
|
| "secp384r1"
|
|
|
|
| "secp521r1"
|
|
|
|
| "curve25519" = "prime256v1",
|
|
|
|
) {
|
|
|
|
return await generateKeyPair("ec", {
|
2020-05-10 03:42:31 -06:00
|
|
|
namedCurve,
|
|
|
|
publicKeyEncoding: {
|
2023-01-12 21:40:33 -07:00
|
|
|
type: "spki",
|
|
|
|
format: "pem",
|
2020-05-10 03:42:31 -06:00
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
2023-01-12 21:40:33 -07:00
|
|
|
type: "pkcs8",
|
|
|
|
format: "pem",
|
2020-05-10 03:42:31 -06:00
|
|
|
cipher: undefined,
|
2021-12-09 07:58:30 -07:00
|
|
|
passphrase: undefined,
|
|
|
|
},
|
2020-05-10 03:42:31 -06:00
|
|
|
});
|
|
|
|
}
|