@trap_stevo/veripath
Version:
The pinnacle of real-time encrypted routing and session-bound communication through a precision-crafted middleware system. Empowering developers to secure every route with dynamic request decryption, intelligent session validation, and seamless encrypted
33 lines (32 loc) • 1.19 kB
JavaScript
;
const crypto = require("crypto");
class RouteSecurityEngine {
constructor(options = {}) {
this.algorithm = "aes-256-gcm";
this.ivLength = options.encryptorLength ?? 12;
this.options = options;
}
async encrypt(data, sessionKey) {
const iv = crypto.randomBytes(this.ivLength);
const cipher = crypto.createCipheriv(this.algorithm, Buffer.from(sessionKey), iv);
const json = JSON.stringify(data);
const encrypted = Buffer.concat([cipher.update(json, "utf8"), cipher.final()]);
const tag = cipher.getAuthTag();
const payload = Buffer.concat([iv, tag, encrypted]);
return payload.toString("base64");
}
async decrypt(encryptedBase64, sessionKey) {
const payload = Buffer.from(encryptedBase64, "base64");
const ciphertext = payload.slice(28);
const tag = payload.slice(12, 28);
const iv = payload.slice(0, 12);
const decipher = crypto.createDecipheriv(this.algorithm, Buffer.from(sessionKey), iv);
decipher.setAuthTag(tag);
const decrypted = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
return JSON.parse(decrypted.toString("utf8"));
}
}
;
module.exports = {
RouteSecurityEngine
};