Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ Following options are available for the JS Obfuscator:
inputFileName: '',
log: false,
numbersToExpressions: false,
numbersToHexadecimal: true,
optionsPreset: 'default',
renameGlobals: false,
renameProperties: false,
Expand Down Expand Up @@ -634,6 +635,7 @@ Following options are available for the JS Obfuscator:
--ignore-imports <boolean>
--log <boolean>
--numbers-to-expressions <boolean>
--numbers-to-hexadecimal <boolean>
--options-preset <string> [default, low-obfuscation, medium-obfuscation, high-obfuscation]
--rename-globals <boolean>
--rename-properties <boolean>
Expand Down Expand Up @@ -1099,7 +1101,7 @@ Type: `boolean` Default: `false`

Enables numbers conversion to expressions

Example:
Example:
```ts
// input
const foo = 1234;
Expand All @@ -1108,6 +1110,26 @@ const foo = 1234;
const foo=-0xd93+-0x10b4+0x41*0x67+0x84e*0x3+-0xff8;
```

### `numbersToHexadecimal`
Type: `boolean` Default: `true`

Enables conversion of integer numbers to hexadecimal format.

When `true`, integer numbers like `1000` will be converted to `0x3e8`.
When `false`, numbers remain in their original decimal format.

Example:
```ts
// input
setInterval(sendHeartbeat, 1000 * 60 * 1);

// output with numbersToHexadecimal: true
setInterval(sendHeartbeat, 0x3e8 * 0x3c * 0x1);

// output with numbersToHexadecimal: false
setInterval(sendHeartbeat, 1000 * 60 * 1);
```

### `optionsPreset`
Type: `string` Default: `default`

Expand Down
4 changes: 4 additions & 0 deletions src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
)
.option('--log <boolean>', 'Enables logging of the information to the console', BooleanSanitizer)
.option('--numbers-to-expressions <boolean>', 'Enables numbers conversion to expressions', BooleanSanitizer)
.option(
'--numbers-to-hexadecimal <boolean>', 'Enables conversion of integer numbers to hexadecimal format',
BooleanSanitizer
)
.option(
'--options-preset <string>',
'Allows to set options preset. ' +
Expand Down
1 change: 1 addition & 0 deletions src/custom-code-helpers/CustomCodeHelperObfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class CustomCodeHelperObfuscator implements ICustomCodeHelperObfuscator {
identifierNamesGenerator: this.options.identifierNamesGenerator,
identifiersDictionary: this.options.identifiersDictionary,
numbersToExpressions: this.options.numbersToExpressions,
numbersToHexadecimal: this.options.numbersToHexadecimal,
simplify: this.options.simplify,
seed: this.randomGenerator.getRawSeed(),
...additionalOptions
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/options/IOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IOptions {
readonly inputFileName: string;
readonly log: boolean;
readonly numbersToExpressions: boolean;
readonly numbersToHexadecimal: boolean;
readonly optionsPreset: TOptionsPreset;
readonly renameGlobals: boolean;
readonly renameProperties: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class NumberLiteralTransformer extends AbstractNodeTransformer {
if (this.numberLiteralCache.has(literalValue)) {
rawValue = <string>this.numberLiteralCache.get(literalValue);
} else {
if (NumberUtils.isCeil(literalValue)) {
if (this.options.numbersToHexadecimal && NumberUtils.isCeil(literalValue)) {
rawValue = NumberUtils.toHex(literalValue);
} else {
rawValue = String(literalValue);
Expand Down
6 changes: 6 additions & 0 deletions src/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ export class Options implements IOptions {
@IsBoolean()
public readonly numbersToExpressions!: boolean;

/**
* @type {boolean}
*/
@IsBoolean()
public readonly numbersToHexadecimal!: boolean;

/**
* @type {TOptionsPreset}
*/
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
inputFileName: '',
log: false,
numbersToExpressions: false,
numbersToHexadecimal: true,
optionsPreset: OptionsPreset.Default,
renameGlobals: false,
renameProperties: false,
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/NoCustomNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
inputFileName: '',
log: false,
numbersToExpressions: false,
numbersToHexadecimal: true,
renameGlobals: false,
renameProperties: false,
renamePropertiesMode: RenamePropertiesMode.Safe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,52 @@ describe('NumberLiteralTransformer', () => {
assert.match(obfuscatedCode, regExp);
});
});

describe('numbersToHexadecimal option', () => {
describe('when numbersToHexadecimal is true (default)', () => {
const regExp: RegExp = /^setInterval\(sendHeartbeat, 0x3e8 \* 0x3c \* 0x1\);$/;

let obfuscatedCode: string;

before(() => {
const code: string = 'setInterval(sendHeartbeat, 1000 * 60 * 1);';

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
numbersToHexadecimal: true,
compact: false
}
).getObfuscatedCode();
});

it('should convert numbers to hexadecimal', () => {
assert.match(obfuscatedCode, regExp);
});
});

describe('when numbersToHexadecimal is false', () => {
const regExp: RegExp = /^setInterval\(sendHeartbeat, 1000 \* 60 \* 1\);$/;

let obfuscatedCode: string;

before(() => {
const code: string = 'setInterval(sendHeartbeat, 1000 * 60 * 1);';

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
numbersToHexadecimal: false,
compact: false
}
).getObfuscatedCode();
});

it('should keep numbers in decimal format', () => {
assert.match(obfuscatedCode, regExp);
});
});
});
});