mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
refactor: remove generating keypair
This commit is contained in:
1 parent
740cbcecb4
commit
307af61726
3 files changed
-56
No files matched your search
@@ -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 }
|
||||
}
|
||||
Reference in new issue
Block a user