|
|
const crypto = require("crypto"); |
|
|
const fs = require("fs"); |
|
|
const path = require("path"); |
|
|
const keyPath = |
|
|
process.env.NODE_ENV === "development" |
|
|
? path.resolve(__dirname, `../../storage/comkey`) |
|
|
: path.resolve( |
|
|
process.env.STORAGE_DIR ?? path.resolve(__dirname, `../../storage`), |
|
|
`comkey` |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommunicationKey { |
|
|
#privKeyName = "ipc-priv.pem"; |
|
|
#pubKeyName = "ipc-pub.pem"; |
|
|
#storageLoc = keyPath; |
|
|
|
|
|
|
|
|
|
|
|
constructor(generate = false) { |
|
|
if (generate) this.#generate(); |
|
|
} |
|
|
|
|
|
log(text, ...args) { |
|
|
console.log(`\x1b[36m[CommunicationKey]\x1b[0m ${text}`, ...args); |
|
|
} |
|
|
|
|
|
#readPrivateKey() { |
|
|
return fs.readFileSync(path.resolve(this.#storageLoc, this.#privKeyName)); |
|
|
} |
|
|
|
|
|
#generate() { |
|
|
const keyPair = crypto.generateKeyPairSync("rsa", { |
|
|
modulusLength: 2048, |
|
|
publicKeyEncoding: { |
|
|
type: "pkcs1", |
|
|
format: "pem", |
|
|
}, |
|
|
privateKeyEncoding: { |
|
|
type: "pkcs1", |
|
|
format: "pem", |
|
|
}, |
|
|
}); |
|
|
|
|
|
if (!fs.existsSync(this.#storageLoc)) |
|
|
fs.mkdirSync(this.#storageLoc, { recursive: true }); |
|
|
fs.writeFileSync( |
|
|
`${path.resolve(this.#storageLoc, this.#privKeyName)}`, |
|
|
keyPair.privateKey |
|
|
); |
|
|
fs.writeFileSync( |
|
|
`${path.resolve(this.#storageLoc, this.#pubKeyName)}`, |
|
|
keyPair.publicKey |
|
|
); |
|
|
this.log( |
|
|
"RSA key pair generated for signed payloads within AnythingLLM services." |
|
|
); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sign(textData = "") { |
|
|
return crypto |
|
|
.sign("RSA-SHA256", Buffer.from(textData), this.#readPrivateKey()) |
|
|
.toString("hex"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
encrypt(textData = "") { |
|
|
return crypto |
|
|
.privateEncrypt(this.#readPrivateKey(), Buffer.from(textData, "utf-8")) |
|
|
.toString("base64"); |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = { CommunicationKey }; |
|
|
|