Skip to content

Project overview

Original Project Structure

Original Code

js
const fs = require("fs");
const { fullName } = require("./lib/user");
const constants = require("./utils/constants");

const sayHi = (firstName, lastName) => {
  const name = fullName(firstName, lastName);
  fs.writeFileSync("sayHi.txt", name);
  console.log(`${constants.greeting} ${name}!`);
};

sayHi();
js
const fullName = (firstName, lastName) => {
  return `${firstName} ${lastName}`;
};
module.exports = { fullName };
js
const greeting = "hi";
module.exports = { greeting };

1. Setup

Installation

bash
npm install -g codefend

You can find more information about the installation process in the installation guide.

Configuration file

bash
cd [your-project-root-folder-goes-here]
codefend -i

This command will create a new configuration file for your project .codefendrc.json.

Refer to the configuration guide or reference for additional details on Codefend configuration.

2. Applying naming convention Required

Project Structure

Simply add the prefix pkg- to each folder and file- to each file that requires the action. Remember to exclude any folders and files that should not undergo this process, and don't forget to update your imports inside your code.

Code

Simply add the prefix l_ to each variable that need to be obfuscated.

js
const l_fs = require("fs");
const { l_fullName } = require("./pkg-lib/file-user");
const l_constants = require("./pkg-utils/file-constants");

const l_sayHi = (l_firstName, l_lastName) => {
  const l_name = l_fullName(l_firstName, l_lastName);
  l_fs.writeFileSync("sayHi.txt", l_name);
  console.log(`${l_constants.l_greeting} ${l_name}!`);
};

l_sayHi();
js
const l_fullName = (l_firstName, l_lastName) => {
  return `${l_firstName} ${l_lastName}`;
};
module.exports = { l_fullName };
js
const l_greeting = "hi";
module.exports = { l_greeting };

Caution: It's not possible to obfuscate all variables and functions.

Be sure to avoid adding the prefix to variables and functions that are not defined in your source code, like those from 3rd party dependencies.

For example, in the code above, we added the prefix "l_" to the variable "fs" because it's defined in our files, but we didn't add the prefix to "writeFileSync" since it's defined in the external library "fs".

3. Obfuscation

To start obfuscating your code, run the following command in your project directory, where you have the configuration file:

bash
codefend -o

For more information about this step, please refer to this guide.

To see a detailed list of all available commands, check out this section.

Output

js
const Ox5 = require("fs");
const { Ox6 } = require("./Ox0/Ox1");
const Ox7 = require("./Ox2/Ox3");

const Ox8 = (Ox9, Ox10) => {
  const Ox11 = Ox6(Ox9, Ox10);
  Ox5.writeFileSync("sayHi.txt", Ox11);
  console.log(`${Ox7.Ox12} ${Ox11}!`);
};

Ox8();
js
const Ox6 = (Ox9, Ox10) => {
  return `${Ox9} ${Ox10}`;
};
module.exports = { Ox6 };
js
const Ox12 = "hi";
module.exports = { Ox12 };
For more information on the different transformation techniques available and how to apply them, please check the transformation [guide](../advanced-usage/transformation/list.md) in the advanced usage section.

4. Distribution

Troubleshooting, Building, and Deployment

After successfully obfuscating your code, you'll have two versions: the original source code and the obfuscated one.

To ensure the obfuscated code works just like the original, run it and perform thorough testing. If you encounter any issues, refer to the troubleshooting guide. Once you're confident it's working as expected, proceed with building and deploying the obfuscated code. This will create a protected version that you can share without revealing your original source code.

For more detailed instructions on building and deploying, check out this section.