What is javascript-obfuscator?
The javascript-obfuscator npm package is a powerful tool for obfuscating JavaScript code, making it difficult for others to read and understand. This is useful for protecting intellectual property, preventing code tampering, and reducing the risk of reverse engineering.
What are javascript-obfuscator's main functionalities?
Basic Obfuscation
This feature allows you to obfuscate basic JavaScript code. The example shows how to obfuscate a simple function that logs 'Hello World' to the console.
const JavaScriptObfuscator = require('javascript-obfuscator');
const obfuscatedCode = JavaScriptObfuscator.obfuscate(
`function hello() { console.log('Hello World'); }`,
{ compact: true, controlFlowFlattening: false }
).getObfuscatedCode();
console.log(obfuscatedCode);
Control Flow Flattening
Control Flow Flattening is a feature that makes the control flow of the code less recognizable. The example shows how to enable this option to further obfuscate the 'hello' function.
const JavaScriptObfuscator = require('javascript-obfuscator');
const obfuscatedCode = JavaScriptObfuscator.obfuscate(
`function hello() { console.log('Hello World'); }`,
{ controlFlowFlattening: true }
).getObfuscatedCode();
console.log(obfuscatedCode);
String Array Encoding
String Array Encoding encodes strings in the code to make them harder to read. The example shows how to encode strings using base64 encoding.
const JavaScriptObfuscator = require('javascript-obfuscator');
const obfuscatedCode = JavaScriptObfuscator.obfuscate(
`function hello() { console.log('Hello World'); }`,
{ stringArray: true, stringArrayEncoding: ['base64'] }
).getObfuscatedCode();
console.log(obfuscatedCode);
Self-Defending
The Self-Defending feature makes the obfuscated code more difficult to modify and tamper with. The example shows how to enable this option.
const JavaScriptObfuscator = require('javascript-obfuscator');
const obfuscatedCode = JavaScriptObfuscator.obfuscate(
`function hello() { console.log('Hello World'); }`,
{ selfDefending: true }
).getObfuscatedCode();
console.log(obfuscatedCode);
Other packages similar to javascript-obfuscator
obfuscator-io-metro-plugin
The obfuscator-io-metro-plugin is a Metro plugin for React Native that uses obfuscator.io to obfuscate JavaScript code. It is specifically designed for React Native projects and integrates seamlessly with the Metro bundler. Compared to javascript-obfuscator, it is more specialized for React Native environments.
uglify-js
UglifyJS is a JavaScript parser, minifier, compressor, and beautifier toolkit. While its primary focus is on minification and compression, it also offers some obfuscation features. Compared to javascript-obfuscator, UglifyJS is more focused on reducing file size and improving performance, with less emphasis on making the code difficult to understand.
#JavaScript obfuscator for Node.js
JavaScript obfuscator for Node.js and free alternative of js-obfuscator (which uses javascriptobfuscator.com) without any limits and sending data on server.
Compatible with ES6.
Tested on Angular2 bundle.
https://gist.github.com/sanex3339/ffc2876123b52e6d11ce45369fd53acf
###Installation
Install the package with NPM and add it to your devDependencies:
npm install --save-dev javascript-obfuscator
###Usage:
var JavaScriptObfuscator = require('javascript-obfuscator');
var obfuscatedCode = JavaScriptObfuscator.obfuscate(
`
(function(){
var variable = 'abc';
console.log(variable);
})();
`,
{
rotateUnicodeArray: false
}
);
console.log(obfuscatedCode);
obfuscate(sourceCode, options)
###sourceCode
Type: string
Default: null
Any valid SourceCode
###options
Type: Object
Default: null
Options for JavaScript obfuscator:
{
rotateUnicodeArray: false
}
###Available options
####rotateUnicodeArray
Type: boolean
Default: true
For more hard understanding of code, during each obfuscation all literal values are stored in array as Unicode codes sequence.
This options will rotate all values inside array on a random value during obfuscation of code, and insert inside source code helper function
which will rotate array values back to their original indexes.
This option affected only on visual code organisation, because we can easily get original array during debug process.
Not recommended for small source code, because helper function will attract attention.