JavScript code obfuscation:
- is used for: "reasons"...
- provides some low-level protecting against: straight-forward debugging, hardening listening or program-flow.
- required to: execute fast, limit 'eval' execution, DOM friendly
best practices also covers minimal D.O.M evaluations: since obfuscated code usually executes few other methods for the same plain input, a good obfuscation algorithm adds fewest evaluated phrases as possible, and called 'eval' method only once.
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
if(0 === n % 311) //311 is is a prime-number, it is not normally a multiplication of any ASCII char (maybe long UNICODE.. TODO: choose larger primer)
char = String.fromCharCode(n / 311);
char = chars.substr(n,1);
var phrase = 'console.log("hello")';
var obfuscated_phrase = "[" + phrase.replace(/./g,toE).replace(/\\,$/,'') + "]";
var translated_plain_phrase = eval(obfuscated_phrase).join('');
" + phrase + "\
" + "To: \
" + obfuscated_phrase + "\
" + "Back To: \
" + translated_plain_phrase + "\
it result with the following output in the Console:
piping the result from this simple obfuscation matrix into Closure Compiler Service or UglifyJS may be interesting to witness, normally both c.compiler and uglifyJS try to "understand" the code by braking it to trees, then parse it using tree-logic permitted-operations resulting smaller trees, then re-parse the trees back to plain code,
so... it will either increase or decrease the complexity of the code using more or fewer transitions, rule-of-thumb is that you should obfuscate your code using three or more chained calls, this will result with a very deep and narrow tree, and UglifyJS or Google Closure-Compiler, will then "work for you" minifying and obfuscating the end-result even more, with minimal, or no-human intervention..