mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: load or create key
This commit is contained in:
1 parent
2cc348687a
commit
03b2993b56
2 files changed
+34
-4
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import fs from 'fs'
|
||||
import { generateKeyPairSync } from 'crypto'
|
||||
|
||||
type KeyPair = {
|
||||
@@ -5,8 +6,26 @@ type KeyPair = {
|
||||
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 keyPair = generateKeyPair()
|
||||
fs.writeFileSync(privateKeyPath, keyPair.privateKey, {
|
||||
mode: 0o600,
|
||||
})
|
||||
fs.writeFileSync(publicKeyPath, keyPair.publicKey, {
|
||||
mode: 0o600,
|
||||
})
|
||||
return keyPair
|
||||
}
|
||||
|
||||
export function generateKeyPair(): KeyPair {
|
||||
const { privateKey, publicKey } = generateKeyPairSync('ed25519', {
|
||||
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
|
||||
modulusLength: 2048,
|
||||
publicKeyEncoding: {
|
||||
type: 'spki',
|
||||
format: 'pem',
|
||||
|
||||
Reference in new issue
Block a user