hippofish/packages/backend/src/misc/gen-key-pair.ts

43 lines
810 B
TypeScript
Raw Normal View History

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