33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
const fs = require("node:fs")
|
|
const path = require("node:path")
|
|
const crypto = require("node:crypto")
|
|
const logger = require("../modules/logger")
|
|
require("colors")
|
|
|
|
function initKeys() {
|
|
if (!isExists("private.pem") && isExists("public.pem")) {
|
|
logger.log(`[${"RSA Keys".blue}] Public key is missing.`)
|
|
fs.unlinkSync("public.pem")
|
|
}
|
|
if (!isExists("public.pem") && isExists("private.pem")) {
|
|
logger.log(`[${"RSA Keys".blue}] Private key is missing`)
|
|
fs.unlinkSync("private.pem")
|
|
}
|
|
if (!isExists("private.pem") && !isExists("public.pem")) {
|
|
logger.log(`[${"RSA Keys".blue}] RSA Keys are missings, generating`)
|
|
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", {
|
|
modulusLength: 2048
|
|
})
|
|
fs.writeFileSync(path.join(__dirname, "..", "keys", "private.pem"), privateKey.export({ type: "pkcs1", format: "pem" }))
|
|
fs.writeFileSync(path.join(__dirname, "..", "keys", "public.pem"), publicKey.export({ type: "spki", format: "pem" }))
|
|
logger.log(`[${"RSA Keys".blue}] RSA Keys generated`)
|
|
}
|
|
}
|
|
|
|
function isExists(filename) {
|
|
return fs.existsSync(path.join(__dirname, "keys", filename))
|
|
}
|
|
|
|
module.exports = {
|
|
initKeys
|
|
} |