0

I'm looking for a Javascript obfuscation tool that will be able to obfuscate specific string literals.

I have a class function like this:

function Mapper() {
    var map = {};
    this.put = function(name, val) {
        map[name] = val;
    };
    this.get = function(name) {
        return map[name];
    };
}

And it's used like this throughout my code:

var mapper = new Mapper();
mapper.put("first", 123);
mapper.put("second", 999);
var value = mapper.get("first");
// etc...

I want to obfuscate the key names - "first", "second" - since they are only relevant during development.
I obviously need these strings to be obfuscated only where they are used in context with the mapper put() or get() methods, but this is not an issue because the keys I use are not used anywhere else in my application.
However, I don't want ALL the string literals in my app to be obfuscated. Not all string literals are used as object property keys.
I wouldn't mind manually defining a list of keys that should be obfuscated if the tool requires me to do so.

How can this be done?

Is there a way to do this with one of the commonly used obfuscation tools like Closure Compiler, YUI, unglifyjs, or anything similar?

Malki
  • 2,335
  • 8
  • 31
  • 61
  • 1
    Why do you need this? Anybody can get keys from any object via `Object.keys` or any `for .. in` loop – Alex Jan 28 '15 at 12:09
  • So for a set of known keys, you want to change every mapper.get(key) [and every mapper.put(key,x) etc.] to mapper.get(obfuscate(key), ...) where obfuscate(key) produces a replacement string that is unrelated to key? And that's *all* you want to do? (You want to also do conventional obfuscation?) – Ira Baxter Jan 28 '15 at 13:04
  • I want to minify the code. While in development it's convenient to have human readable keys like "first", in production nobody needs them and they just take up space – Malki Jan 28 '15 at 13:05
  • So for example every mapper.get("first") I want to get mapper.get("f"). I do also want conventional obfuscation, so actually it would be m.get("f"). And if at all possible (via closure compiler for example) I'd like to also obfuscate the "get" function, so the final result would be m.g("f") – Malki Jan 28 '15 at 13:08
  • 1
    This optimization will probably only provide minimal size improvements due to GZIP. See https://github.com/google/closure-compiler/wiki/FAQ#closure-compiler-inlined-all-my-strings-which-made-my-code-size-bigger-why-did-it-do-that – Chad Killingsworth Jan 28 '15 at 18:53

1 Answers1

1

The Closure Compiler has both a "ReplaceStrings" and support for replacing "id generators". Without more details, I would assume the id generator replacement is what you want.

John
  • 5,443
  • 15
  • 21