> ## Documentation Index
> Fetch the complete documentation index at: https://docs.js-confuser.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrity

> Integrity ensures the source code is unchanged.

* Option name: `"lock.integrity"`

* Option values: `true/false/0-1/Function`

***

### Input / Output

This example showcases how `Integrity` transforms the code. Try it out by changing the input code and see changes apply in real-time.

```js title="Input.js" lines interactive-mode="obfuscate" theme={null}
// Input.js
function protectedFunction() {
  console.log('This code is protected');
}

protectedFunction(); // "This code is protected"

// Output.js
function jb598RU(jb598RU, JKVP7D) {
  JKVP7D |= 0;
  var lcjpEql = (jb598RU & 0x003fffff) * JKVP7D;
  if (jb598RU & 0xffc00000) lcjpEql += ((jb598RU & 0xffc00000) * JKVP7D) | 0;
  return lcjpEql | 0;
}
var JKVP7D = Math.imul || jb598RU;
function lcjpEql(jb598RU, lcjpEql) {
  var UXqnfXh = 0xdeadbeef ^ lcjpEql,
    eNMJddo = 0x41c6ce57 ^ lcjpEql;
  for (var cBtcj9 = 0, nJG7oY; cBtcj9 < jb598RU.length; cBtcj9++) {
    nJG7oY = jb598RU.charCodeAt(cBtcj9);
    UXqnfXh = JKVP7D(UXqnfXh ^ nJG7oY, 2654435761);
    eNMJddo = JKVP7D(eNMJddo ^ nJG7oY, 1597334677);
  }
  UXqnfXh =
    JKVP7D(UXqnfXh ^ (UXqnfXh >>> 16), 2246822507) ^
    JKVP7D(eNMJddo ^ (eNMJddo >>> 13), 3266489909);
  eNMJddo =
    JKVP7D(eNMJddo ^ (eNMJddo >>> 16), 2246822507) ^
    JKVP7D(UXqnfXh ^ (UXqnfXh >>> 13), 3266489909);
  return 4294967296 * (2097151 & eNMJddo) + (UXqnfXh >>> 0);
}
function UXqnfXh(
  jb598RU,
  JKVP7D,
  UXqnfXh = new RegExp(' |\\n|;|,|\\{|\\}|\\(|\\)|\\.|\\[|\\]', 'g'),
) {
  var eNMJddo = jb598RU.toString().replace(UXqnfXh, '');
  return lcjpEql(eNMJddo, JKVP7D);
}
function eNMJddo() {
  console.log('This code is protected');
}
function cBtcj9() {
  var I8cFpOp = cBtcj9.fbhBa2Q || (cBtcj9.fbhBa2Q = UXqnfXh(eNMJddo, 4349025));
  if (I8cFpOp === 4853002617990608) {
    return eNMJddo(...arguments);
  } else {
    while (true) {}
  }
}
cBtcj9();
```

***

### How is this possible?

JavaScript has a sneaky method to view the source code any function. Calling `Function.toString()` on any function reveals the raw source code.

Integrity uses a hashing algorithm on the obfuscated code during the obfuscation-phase. The obfuscator then places checksum functions throughout the output code to verify it's unchanged at runtime.

An additional RegEx is utilized to remove spaces, newlines, braces, and commas. This ensures the hash isn't too sensitive.

### Tamper Detection

If tampering is detected, the `lock.countermeasures` function will be invoked. If you don't provide a `lock.countermeasures` function, the default behavior is to crash the program.

[Learn more about the countermeasures function](Countermeasures.md)

***

### Custom Implementation

#### `options.lock.integrity(fnName)`

Control which functions are changed. Returns a `boolean`.

| Parameter | Type     | Description                               |
| --------- | -------- | ----------------------------------------- |
| `fnName`  | `string` | The function name proposed to be changed. |

***

### Usage Example

The provided code example will obfuscate the file `input.js` and write the output to a file named `output.js`.

```js title="Usage Example" lines theme={null}
import JSConfuser from "js-confuser";
import {readFileSync, writeFileSync} from "fs";

// Read input code
const sourceCode = readFileSync("input.js", "utf8");
const options = {
  target: 'browser',
  lock: {
    integrity: true,
  },
};

JSConfuser.obfuscate(sourceCode, options).then((result)=>{
  // Write output code
  writeFileSync("output.js", result.code);
}).catch(err=>{
  // Error occurred
  console.error(err);  
});
```

***

#### Enabled In

* [High Preset](/presets/high): No
* [Medium Preset](/presets/medium): No
* [Low Preset](/presets/low): No

***

### Potential Issues

If you decide to use Integrity, consider the following:

1. Any build-tools must not modify the locked code. The code can't be changed after JS-Confuser is applied.
2. `Function.toString()` functionality may not be enabled in your environment (bytenode)

***

#### See Also

* [Countermeasures](/options/countermeasures)
