DEV Community

Dhairya Shah
Dhairya Shah

Posted on

How to protect your JavaScript Code

Hello, I am back with another article and it will be fun

Let's get's started,

What does Obfuscate mean?

  • In JavaScript, obfuscation means to protect code which is exposed on the web

Some Benefits of Obfuscation

  • Helps to protect secret credentials for attackers
  • Helps to hide some functions
  • Prevent people from copying or modifying your code without authorization.
  • The obfuscated JavaScript will be way larger and difficult to understand

How to obfuscate?

  • Code not obfuscated
function hello(){
    console.log('Hello world')
}

hello()
Enter fullscreen mode Exit fullscreen mode
  • Obfuscated Code
(function(_0xfa9ce1, _0x293dc1) {
    var _0x5be444 = _0x3aed,
        _0x338d24 = _0xfa9ce1();
    while (!![]) {
        try {
            var _0x25e185 = parseInt(_0x5be444(0x159)) / 0x1 + -parseInt(_0x5be444(0x152)) / 0x2 * (parseInt(_0x5be444(0x156)) / 0x3) + -parseInt(_0x5be444(0x151)) / 0x4 * (-parseInt(_0x5be444(0x153)) / 0x5) + -parseInt(_0x5be444(0x15a)) / 0x6 * (parseInt(_0x5be444(0x150)) / 0x7) + -parseInt(_0x5be444(0x14e)) / 0x8 + parseInt(_0x5be444(0x154)) / 0x9 * (parseInt(_0x5be444(0x157)) / 0xa) + -parseInt(_0x5be444(0x14f)) / 0xb * (-parseInt(_0x5be444(0x158)) / 0xc);
            if (_0x25e185 === _0x293dc1) break;
            else _0x338d24['push'](_0x338d24['shift']());
        } catch (_0x460298) {
            _0x338d24['push'](_0x338d24['shift']());
        }
    }
}(_0x49c5, 0xf38c8));

function hello() {
    var _0x590815 = _0x3aed;
    console['log'](_0x590815(0x155));
}

function _0x3aed(_0x7ab531, _0x4d788b) {
    var _0x49c58c = _0x49c5();
    return _0x3aed = function(_0x3aed39, _0x5113e2) {
        _0x3aed39 = _0x3aed39 - 0x14e;
        var _0x3d5157 = _0x49c58c[_0x3aed39];
        return _0x3d5157;
    }, _0x3aed(_0x7ab531, _0x4d788b);
}

function _0x49c5() {
    var _0x1ab529 = ['49tFyzVa', '4QHsemb', '325768BSBOlC', '8641220FeGZkj', '9081ERaIXL', 'Hello\x20world', '3yHbwCr', '4400XMVpPJ', '24fWLzKR', '1213114zYLYOQ', '1345902AroHuK', '9509096jkUgBM', '2936989JRGabm'];
    _0x49c5 = function() {
        return _0x1ab529;
    };
    return _0x49c5();
}
hello();
Enter fullscreen mode Exit fullscreen mode
  • Test them both will have same output

How to obfuscate?

  • Write javascript code
  • Copy and paste to Obfuscator.io
  • And replace obfuscated code with the original

Make sure to check out my Youtube Channel and subscribe to it

Top comments (5)

Collapse
 
jayjeckel profile image
Jay Jeckel

Obfuscation is at best rude and more often it is actually harmful to your users. The example in this article perfectly demonstrates why. Look at all the useless operations that are being run in the obfuscated form of the code. Those operations are unneeded, waste your users' cpu cycles, and cost your users money in the form of more electricity for no benefit to anyone.

while (!![])

That line alone should show any developer that obfuscation is harmful. If a developer put that code in one of my codebases, I'd fire them on the spot. And it being put there by an automated process doesn't make the code smell any less.

If one absolutely has to obfuscate their code, then, please, as a developer and user, I beg of you to use an obfuscation library that doesn't add pointless code that does nothing other than waste the user's time and money.

Collapse
 
_genjudev profile image
Larson

Don't try this at home.

Helps to protect secret credentials for attackers

no.

Helps to hide some functions

no.

Prevent people from copying or modifying your code without authorization.

no. just makes it harder. There is no authorization.

The obfuscated JavaScript will be way larger and difficult to understand

yes.

Don't store credentials, don't store any critical stuff.

Collapse
 
kamilbugnokrk profile image
Kamil Bugno

"The obfuscated JavaScript will be way larger"
Why is it a benefit? As a result, the user will need to download more data to display the page so it seems to be a disadvantage

Collapse
 
ironcladdev profile image
Conner Ow • Edited

doing this is absolute garbage. Imagine how hard your JS has to work when it comes to complex operations!

The goal is to optimize. Yes, this could protect your JS readability a bit, but sheesh.

To be honest, anyone could just rename all the variables with a search-and-replace engine and your code just looks like it was coded by a three-year-old.

If you want protection, use a backend server.

Collapse
 
khauri profile image
Khauri

Prevent people from copying or modifying your code without authorization

According to a certain governor, looking at a web site's source code is illegal hacking, and no one would ever do anything against the law, right?