feat: generate both RSA and Ed25519 host keys

This commit is contained in:
2026-07-13 17:00:39 +07:00
parent 927a953d43
commit 3c3531d1f7
+28 -16
View File
@@ -3,25 +3,37 @@ import path from "path";
import crypto from "crypto";
import sshpk from "sshpk";
export function ensureHostKeys(configDir: string): void {
const keyPath = path.join(configDir, "id_rsa");
if (fs.existsSync(keyPath)) return;
function generateAndSave(keyPath: string, type: "rsa" | "ed25519"): void {
console.log(`Generating ${type} host key...`);
console.log("Generating host keys...");
const key = crypto.generateKeyPairSync("rsa", {
const { privateKey } =
type === "rsa"
? crypto.generateKeyPairSync("rsa", {
modulusLength: 4096,
publicKeyEncoding: {
type: "pkcs1",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
},
publicKeyEncoding: { type: "pkcs1", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" },
})
: crypto.generateKeyPairSync("ed25519", {
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" },
});
const keyPem = sshpk.parsePrivateKey(key.privateKey, "pem");
const keyParsed = sshpk.parsePrivateKey(keyPem.toString("pem"));
const parsed = sshpk.parsePrivateKey(privateKey, "pem");
fs.writeFileSync(keyPath, parsed.toString("openssh"), { mode: 0o600 });
fs.chmodSync(keyPath, 0o600);
}
fs.writeFileSync(keyPath, keyParsed.toString("openssh"));
export function ensureHostKeys(configDir: string): Buffer[] {
const keys: Array<{ file: string; type: "rsa" | "ed25519" }> = [
{ file: "id_rsa", type: "rsa" },
{ file: "id_ed25519", type: "ed25519" },
];
return keys.map(({ file, type }) => {
const keyPath = path.join(configDir, file);
if (!fs.existsSync(keyPath)) {
generateAndSave(keyPath, type);
}
return fs.readFileSync(keyPath);
});
}