Search code examples
javascriptgulpobfuscation

Good way to obfuscate Javascript code in Gulp


This question knowing that obfuscation is by no means a strong way to protect code...

Using Gulp, I'm looking for a way to prevent my app's content to appear in a too obvious manner. Not manipulating sensitive data, but I'd still not want my minified code to look too obvious to modify.

Been trying gulp-minify and gulp-uglify, but either my use of them is wrong, either they don't fill my need.

Needs being: - function renaming - variable renaming - string obfuscation (at least prevent the string from being human readable at first glance) - not more than 2x the storage needs

What would be the suggested approaches, leads, plugins?

Thanks in advance,


Solution

  • So far, the most effective (in my case) is to pipe the following code, which just applies character rotation:

        function obfuscate(text, key, n = 126) {
            // return String itself if the given parameters are invalid
            if (!(typeof(key) === 'number' && key % 1 === 0)
                || !(typeof(key) === 'number' && key % 1 === 0)) {
                return text.toString();
            }
    
            var chars = text.toString().split('');
    
            for (var i = 0; i < chars.length; i++) {
                var c = chars[i].charCodeAt(0);
    
                if (c <= n) {
                    chars[i] = String.fromCharCode((chars[i].charCodeAt(0) + key) % n);
                }
            }
    
            return chars.join('');
        },
    
        function defuse(text, key, n = 126) {
            // return String itself if the given parameters are invalid
            if (!(typeof(key) === 'number' && key % 1 === 0)
                || !(typeof(key) === 'number' && key % 1 === 0)) {
                return text.toString();
            }
    
            return obfuscate(text.toString(), n - key);
        }