refactor: remove generating keypair

This commit is contained in:
Justin Lin committed 2025-05-05 19:55:41 +08:00
1 parent 740cbcecb4
commit 307af61726
3 files changed
-56

No files matched your search

-39
View File
@@ -1,39 +0,0 @@
import fs from 'fs'
import path from 'path'
import ssh2 from 'ssh2'
const {
utils: { generateKeyPairSync },
} = ssh2
type KeyPair = {
privateKey: string
publicKey: string
}
export function loadOrCreateKeyPair(privateKeyPath: string): KeyPair {
const publicKeyPath = `${privateKeyPath}.pub`
if (fs.existsSync(privateKeyPath) && fs.existsSync(publicKeyPath)) {
const privateKey = fs.readFileSync(privateKeyPath, 'utf8')
const publicKey = fs.readFileSync(publicKeyPath, 'utf8')
return { privateKey, publicKey }
}
const dir = path.dirname(privateKeyPath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
const keyPair = generateKeyPair()
fs.writeFileSync(privateKeyPath, keyPair.privateKey, {
mode: 0o600,
})
fs.writeFileSync(publicKeyPath, keyPair.publicKey, {
mode: 0o600,
})
return keyPair
}
export function generateKeyPair(): KeyPair {
const { private: privateKey, public: publicKey } =
generateKeyPairSync('ed25519')
return { privateKey, publicKey }
}