"use strict";
|
|
const DEFAULT_KEY = "jms-gc110n-log-obfuscate-default";
|
|
function getKey() {
|
return process.env.JMS_LOG_KEY || DEFAULT_KEY;
|
}
|
|
function obfuscate(text) {
|
if (!text) return text;
|
const key = Buffer.from(getKey(), "utf-8");
|
const data = Buffer.from(text, "utf-8");
|
const out = Buffer.alloc(data.length);
|
for (let i = 0; i < data.length; i++) {
|
out[i] = data[i] ^ key[i % key.length];
|
}
|
return "[OBF]" + out.toString("base64");
|
}
|
|
function deobfuscate(encoded) {
|
if (!encoded) return encoded;
|
if (encoded.startsWith("[OBF]")) {
|
encoded = encoded.slice(5);
|
}
|
const key = Buffer.from(getKey(), "utf-8");
|
const data = Buffer.from(encoded, "base64");
|
const out = Buffer.alloc(data.length);
|
for (let i = 0; i < data.length; i++) {
|
out[i] = data[i] ^ key[i % key.length];
|
}
|
return out.toString("utf-8");
|
}
|
|
module.exports = { obfuscate, deobfuscate };
|