Generate Key Pair
Overview
Public Key Format
Private Key Format
Example Implementation
import crypto from "crypto";
interface PublicKeyJWKS {
x: string;
y: string;
kid: string;
}
function generateKeyPair() {
const keypair = crypto.generateKeyPairSync("ec", {
namedCurve: "prime256v1",
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" },
});
const lines: string[] = keypair.publicKey.trim().split("\n");
const x = lines[1];
const y = lines[2];
const kid = "kid-for-your-key-pair";
const publicKeyJWKS: PublicKeyJWKS = {
x,
y,
kid,
};
const privateKey: string = keypair.privateKey;
console.log({ publicKeyJWKS, privateKey });
...
}Output Example
Last updated