Generating RSA (PEM) Private Keys in Node.js

16 de mai. de 2024

Learn how to generate RSA private keys in PEM format using Node.js.

import * as crypto from "node:crypto";

function generateTestPrivateKey(): string {
  // Generate a test private key using Node.js crypto
  const { privateKey } = crypto.generateKeyPairSync("rsa", {
    modulusLength: 2048, // Generate a key with a 2048 bit modulus
    publicKeyEncoding: {
      type: "spki", // Recommended to use spki for public key encoding
      format: "pem",
    },
    privateKeyEncoding: {
      type: "pkcs8", // Ensure PKCS#8 format
      format: "pem",
    },
  });

  return privateKey; // Returns the privateKey as a PKCS#8 PEM string
}

console.log(generateTestPrivateKey());