Why Randomize Variable Names?
If you are using UglifyJS to uglify your JavaScript code, you are using it only to serve a single purpose – to make it hard for anyone to peek into your code. But if you have minor changes to the code, variable names still come out pretty much same. So it is extremely easy for someone to beautify your code and use diff tools to compare the change and defeats the purpose.
How to Randomize Variable Names?
Here is a quick way to randomize the variable names each time you run UglifyJS. This way when you beautify and diff the two versions, you will see diffs almost everywhere and makes little more hard to pin point the changed code.
I assume you are using UglifyJS as a node module. Go to UglifyJS node module location > lib > scope.js file. At the bottom, you would see a definition of function base54. Replace the whole definition with the following code and you are all set.
For UglifyJS version 3.14.1, use following code. For previous version, scroll down.
var base54 = (function() {
var freq = Object.create(null);
// Generate Min, Max and Random Number
var max = 50, min = 0;
var rnd = Math.floor(Math.random()*(max-min+1)+min);
function init(chars) {
var array = [];
for (var i = 0; i < chars.length; i++) {
var ch = chars[i];
array.push(ch);
freq[ch] = -1e-2 * i;
}
return array;
}
var digits = init("0123456789");
var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
var chars, frequency;
function reset() {
chars = null;
frequency = Object.create(freq);
}
base54.consider = function(str, delta) {
for (var i = str.length; --i >= 0;) {
frequency[str[i]] += delta;
}
};
function compare(a, b) {
return frequency[b] - frequency[a];
}
base54.sort = function() {
chars = leading.sort(compare).concat(digits).sort(compare);
};
base54.reset = reset;
reset();
function base54(num) {
// Add random number
num = num + rnd;
var ret = leading[num % 54];
for (num = Math.floor(num / 54); --num >= 0; num >>= 6) {
ret += chars[num & 0x3F];
}
return ret;
}
return base54;
})();
For UglifyJS version 3.11.6 and below, use the following code.
var base54 = (function() {
var freq = Object.create(null);
// Generate Min, Max and Random Number
var max = 50, min = 0;
var rnd = Math.floor(Math.random()*(max-min+1)+min);
function init(chars) {
var array = [];
for (var i = 0, len = chars.length; i < len; i++) {
var ch = chars[i];
array.push(ch);
freq[ch] = -1e-2 * i;
}
return array;
}
var digits = init("0123456789");
var leading = init("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
var chars, frequency;
function reset() {
frequency = Object.create(freq);
}
base54.consider = function(str, delta) {
for (var i = str.length; --i >= 0;) {
frequency[str[i]] += delta;
}
};
function compare(a, b) {
return frequency[b] - frequency[a];
}
base54.sort = function() {
chars = leading.sort(compare).concat(digits.sort(compare));
};
base54.reset = reset;
reset();
function base54(num) {
var ret = "", base = 54;
// Add random number
num = num + rnd;
num++;
do {
num--;
ret += chars[num % base];
num = Math.floor(num / base);
base = 64;
} while (num > 0);
return ret;
}
return base54;
})();
How this Code Works?
Above code shows commented code. I changed only those lines in the code. It adds a random number between 0 and 50 which will work as a seed. You may adjust this seed based on your need.
UglifyJS Version
Above script is tested with following UglifyJS versions.
- 3.5 – February, 2019
- 3.9.4 – June, 2020
- 3.11.6 – November, 2020
- 3.14.1 – September, 2021
Leave a Reply