mirror of
https://github.com/YuzuZensai/TrollSSH.git
synced 2026-09-13 16:39:06 +00:00
✨ feat: generate both RSA and Ed25519 host keys
This commit is contained in:
+30
-18
@@ -3,25 +3,37 @@ import path from "path";
|
|||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
import sshpk from "sshpk";
|
import sshpk from "sshpk";
|
||||||
|
|
||||||
export function ensureHostKeys(configDir: string): void {
|
function generateAndSave(keyPath: string, type: "rsa" | "ed25519"): void {
|
||||||
const keyPath = path.join(configDir, "id_rsa");
|
console.log(`Generating ${type} host key...`);
|
||||||
if (fs.existsSync(keyPath)) return;
|
|
||||||
|
|
||||||
console.log("Generating host keys...");
|
const { privateKey } =
|
||||||
const key = crypto.generateKeyPairSync("rsa", {
|
type === "rsa"
|
||||||
modulusLength: 4096,
|
? crypto.generateKeyPairSync("rsa", {
|
||||||
publicKeyEncoding: {
|
modulusLength: 4096,
|
||||||
type: "pkcs1",
|
publicKeyEncoding: { type: "pkcs1", format: "pem" },
|
||||||
format: "pem",
|
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
||||||
},
|
})
|
||||||
privateKeyEncoding: {
|
: crypto.generateKeyPairSync("ed25519", {
|
||||||
type: "pkcs8",
|
publicKeyEncoding: { type: "spki", format: "pem" },
|
||||||
format: "pem",
|
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
||||||
},
|
});
|
||||||
});
|
|
||||||
|
|
||||||
const keyPem = sshpk.parsePrivateKey(key.privateKey, "pem");
|
const parsed = sshpk.parsePrivateKey(privateKey, "pem");
|
||||||
const keyParsed = sshpk.parsePrivateKey(keyPem.toString("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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user