Skip to content

Instantly share code, notes, and snippets.

@Lama3L9R
Created November 4, 2023 20:43
Show Gist options
  • Save Lama3L9R/16e84970e357ed77d72b3fb02d7c5fce to your computer and use it in GitHub Desktop.
Save Lama3L9R/16e84970e357ed77d72b3fb02d7c5fce to your computer and use it in GitHub Desktop.
obfuscator.js | originally from lowbyteproductions/JavaScript-Is-Weird/blob/master/index.js
const zero = '([]&[])';
const one = '(-~[])';
const negtiveOne = '(~[])';
const isPrime = n => {
if (n <= 2) return true;
for (let i = 2; i ** 2 <= n; i++) {
if (n % i === 0) return false;
}
return true;
}
const nextPrime = n => {
if (!n) return 2;
let i = n + 1;
while (true) {
if (isPrime(i)) return i;
i++;
}
}
const primeFactors = n => {
if (n <= 2) return [n];
let factors = [];
while (!isPrime(n)) {
let p = nextPrime();
while (n % p !== 0) {
p = nextPrime(p);
}
factors.push(p);
n /= p;
}
return [...factors, n];
}
const numberCache = {}
const constructNewNumber = n => {
if (n > 0) {
return Array.from({ length: n }, () => one).join('+');
} else {
return Array.from({ length: n }, () => negtiveOne).join('+');
}
}
const number = n => {
if (n === 0) return zero;
if (n === 1) return one;
if (n === -1) return negtiveOne;
if (n in numberCache) return numberCache[n];
const factors = primeFactors(n);
for (const f of factors) {
if (f in numberCache) continue;
numberCache[f] = `(${constructNewNumber(f)})`;
}
return factors.map(f => numberCache[f]).join('*');
}
// C
const map = {};
const fromString = s =>s.split('').map(x => {
if (!(x in map)) {
const charCode = x.charCodeAt(0);
return `([]+[])[${fromString('constructor')}][${fromString('fromCharCode')}](${number(charCode)})`;
}
return map[x];
}).join('+');
map.a = `(''+!{})[${number(1)}]`; // from false
map.b = `({}+[])[${number(2)}]`;
map.o = `({}+[])[${number(1)}]`;
map.e = `({}+[])[${number(4)}]`;
map.c = `({}+[])[${number(5)}]`;
map.t = `(""+!-{})[${number(0)}]`;
map[' '] = `({}+[])[${number(7)}]`;
map.l = `(''+!{})[${number(2)}]`; // from false
map.f = `(''+!{})[${number(0)}]`; // from false
map.s = `(''+!{})[${number(3)}]`;// from false
map.r = `(''+!-{})[${number(1)}]`;// from true
map.u = `(''+!-{})[${number(2)}]`; // from true
map.i = `((+!![]/+[])+[])[${number(3)}]`;
map.N = `(''+(${number(0)}/${number(0)}))[${number(0)}]` // from NaN
map.n = `((+!![]/+[])+[])[${number(4)}]`;
map.S = `([]+([]+[])[${fromString('constructor')}])[${number(9)}]`;
map.g = `([]+([]+[])[${fromString('constructor')}])[${number(14)}]`;
map.p = `([]+(/-/)[${fromString('constructor')}])[${number(14)}]`;
map['\\'] = `(/\\\\/+[])[${number(1)}]`;
map.d = `(${number(13)})[${fromString('toString')}](${number(14)})`;
map.h = `(${number(17)})[${fromString('toString')}](${number(18)})`;
map.m = `(${number(22)})[${fromString('toString')}](${number(23)})`;
map.C = `globalThis[${fromString("atob")}](${map.b} + ${number(0)} + ${map.N})[${number(1)}]`
map['-'] = `(~[]+[])[${number(0)}]`
const compile = code => `(()=>{})[${fromString('constructor')}](${fromString(code)})()`;
@Lama3L9R
Copy link
Author

Lama3L9R commented Nov 7, 2023

Using globalThis is a bad idea at all. But using atob is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment