What is Obfuscation?

Obfuscation is the technique to hide the intended meaning of message/code/text by making the message difficult to understand, usually with confusing and ambiguous language.

For e.g. let's say this is our function in JavaScript.


function test(name) {
 console.log('Hello, ' + name); 
} 
hello('Yogesh');

For that, this is obfuscated code:


eval(function(p,a,c,k,e,d){e=function(c){return c};if(!''.replace(/^/,String)){while(c--){d[c]=k[c]||c}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('2 1(0){3.7('4, '+0)}5('6');',8,8,'name|test|function|console|Hello|hello|Yogesh|log'.split('|'),0,{}))

Remember that the obfuscated code depends on which tool you use to obfuscate your code.

There are many tools available to obfuscate the JS code for e.g. Google Closure Compiler

You can also install tool on your local machine. In this article, I will go through the step by step guide on how to install obfuscate tool on your local machine.

Step 1

Make sure you have node and npm are installed on your machine. Check the version using this command:


node --version

npm --version

Step 2

The obfuscator tool should be installed globally. So, we can use -g for that.

Use npm with the -g parameter and sudo (admin user) to install it.


sudo npm install -g  javascript-obfuscator

// it will ask for admin password

It will get installed in few seconds and you'll get a similar message like this one:


+ [email protected]
added 110 packages from 160 contributors 
in 5.6s

Step 3

Use obfuscator tool to obfuscate your JS code.

Go the folder where your JS code file is and type this command in CLI.


javascript-obfuscator .

You'll see the new obfuscated file in the same folder within seconds.

That's it.

There are custmization options available to use.

Visit the npm plugin page to find out more: https://www.npmjs.com/package/javascript-obfuscator

0 Comments

Leave a reply

Your email address will not be published. required fields are marked *