I need to mangle (obfuscate) JavaScript strings using hexadecimal encoding.
Source code:
var a = 'a';
Mangled code:
var a = '\x61';
It is easy to convert string to a hexadecimal value:
var map = {
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
};
var hex = function (str) {
var result = '';
for (var i = 0, l = str.length; i < l; i++) {
var char = str[i];
if (map[char]) {
result += map[char];
} else if ('\\' == char) {
result += '\\' + str[++i];
} else {
result += '\\x' + str.charCodeAt(i).toString(16);
}
}
return result;
};
But when I add this string to the output file I get:
var a = '\\x61';
P.S. I use esprima/escodegen/estraverse to work with AST.