Created
June 24, 2019 18:45
-
-
Save icedream/ebcc4adad9d5414533b7858d72db410e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @author Carl Kittelberger | |
// Because I am the only one insane enough to do this voluntarily for a joke. | |
const n = 0; | |
function notReallyObfuscateNumber(num) { | |
return '0' + num.toString(8); | |
} | |
const numberArrayObfuscationUnicodeOffset = 0x29; | |
function obfuscateNumberArray(nums) { | |
const offset = Math.min(...nums); | |
const top = Math.max(...nums)-offset; | |
const offsetAddition = offset > 0 ? `+${notReallyObfuscateNumber(offset)}` : ''; | |
return `[...${JSON.stringify(nums.map(num => String.fromCharCode(numberArrayObfuscationUnicodeOffset+num-offset)).join(''))}].map(a=>a.charCodeAt(0)-${notReallyObfuscateNumber(numberArrayObfuscationUnicodeOffset)}${offsetAddition})` | |
} | |
function obfuscateString(str) { | |
let characterArray = str.split(''); | |
let characterCodes = []; | |
let minCharacterCode = +Infinity; | |
let maxCharacterCode = -Infinity; | |
for (let i = 0; i < str.length; i++) { | |
let code = str.charCodeAt(i); | |
characterCodes.push(code); | |
if (minCharacterCode > code) { | |
minCharacterCode = code; | |
} else if (maxCharacterCode < code) { | |
maxCharacterCode = code; | |
} | |
} | |
let seed = str.length; | |
let offset = minCharacterCode; | |
let span = maxCharacterCode; | |
let foundSomethingThatWorks = false; | |
let activeCharacterSet = []; | |
let indices = []; | |
for (var c = span; !foundSomethingThatWorks && c > 0; c--) { | |
seed = str.length; | |
do { | |
seed++; | |
activeCharacterSet.length = seed; | |
activeCharacterSet.fill(undefined); | |
activeCharacterSet = activeCharacterSet.map((a,b)=>offset+(b*c)%span); | |
//console.log({ c, seed }); | |
indices = []; | |
foundSomethingThatWorks = true; | |
for (code of characterCodes) { | |
const activeCharacterSetIndex = activeCharacterSet.indexOf(code); | |
if (activeCharacterSetIndex < 0) { | |
foundSomethingThatWorks = false; | |
break; | |
} | |
indices.push(activeCharacterSetIndex); | |
} | |
} while (!foundSomethingThatWorks && seed < 1000); | |
if (foundSomethingThatWorks) break; | |
} | |
// DEBUG | |
//const seedArray = `[...[${','.repeat(seed)}]]`; | |
const seedArray = `(_=[],_[${notReallyObfuscateNumber(seed)}]=0,_.fill(0))`; | |
const outputCode=` | |
[ | |
${seedArray} | |
.map((_,a)=> | |
${notReallyObfuscateNumber(offset)} | |
+a | |
*${notReallyObfuscateNumber(c)} | |
%${notReallyObfuscateNumber(span)} | |
), | |
${obfuscateNumberArray(indices)} | |
].reduce((o,a)=> | |
o | |
? | |
a | |
.map(b=>String.fromCharCode(o[b])) | |
.join('') | |
: | |
a | |
,0) | |
` | |
.replace(/[\t\r\n]+/mg,''); | |
const actualOutput = eval(outputCode); | |
if (actualOutput != str) | |
{ | |
console.error("Invalid code:", outputCode); | |
console.error("Output was:", actualOutput); | |
throw new Error("Output is not matching input"); | |
} | |
return foundSomethingThatWorks ? outputCode /*indices*/ : undefined; | |
} | |
const output = obfuscateString(process.argv[2]); | |
process.stdout.write(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment