Search code examples
javascriptobfuscationminifyuglifyjs

Semi-obfuscate/uglify JavaScript


I know about JS minfiers, obfuscators and minifiers. I was wondering if there is any existing tool (or any fast-to-code solution) to partially obfuscate JavaScript. By partially I mean that it should become difficult to read, but not appear as uglified/minified. It should keep indentation, but lose comments, and partially change variable names, making them unclear without converting them to "a, b, c" like an obfuscator.

The purpose of this could be to take an explicit and reusable code and make it implicit and difficult to be reused by other people, without making it impossible to work with for yourself.

Any idea from where to start to achieve this ? Maybe editing an existing obfuscator ?


Solution

  • [This answer is a direct response to OP's request].

    Semantic Designs JavaScript obfuscator will do what you want, but you'll need two passes.

    On the first pass, run it as obfuscator; it will rename identifiers (although you can control how much or how that is done), strip whitepspace and comments. If you limit its ability to rename the identifiers, you lose some the strength of the obfuscator but that's your choice.

    On the second pass, run it as a prettyprinter; it will introduce nice indentation again. (In fact, the idea for obfsucation came from building a prettyprinter; if you can print-pretty, surely it is easy to print-ugly).

    From the point of view of working with the code, you are better off working with your master copy any way you like, complete with your indentation and nice commentary as documentation. When you are ready to obfsucate, you run the obfuscator, shipping the obfuscated result. Errors reported in the obfuscated result that involve obfuscated names can be mapped back to the original names, using the map of obfuscated <--> original names produced during the obfuscation step.

    This a product of my company. I'd provide a link but SO hates it when I do that, so you'll have to find it via my bio or googling.

    PS: It works exactly as @georg suggests, by parsing to an AST, mangling, and prettyprinting. It doesn't use esprima.