Skip to content

Instantly share code, notes, and snippets.

@icedream
Created June 20, 2021 02:19
Show Gist options
  • Save icedream/e7fd9de1e5a684642da2a22c96beac98 to your computer and use it in GitHub Desktop.
Save icedream/e7fd9de1e5a684642da2a22c96beac98 to your computer and use it in GitHub Desktop.
Some crappy string obfuscation thing
function _arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
encode = (s) => {
var charmap = [...s].reduce((a,b,c)=>({...a,[b]:[...a[b]||[],c]}),{});
var sorted = Object.keys(charmap).sort().reduce((o,k)=>({...o,[k]:charmap[k]}),{});
console.log({charmap, sorted});
var v = Object.entries(sorted).reduce((a,[b,c])=>[...a,b.charCodeAt(0),c.length,...c],[]);
var a = new Uint8Array(v);
return _arrayBufferToBase64(a);
}
decode = (a) => {
a=atob(a);
var b='';
var c=0;
return new Uint8Array(a.length)
.map((_,d)=>a.charCodeAt(d))
.reduce((d,e)=>{
if(c<0){
c=e;
return d;
}else if(c===0){
b=String.fromCharCode(e)
}else{
f=e;
if(d.length<f+1){
d=[...d,...new Array(f-d.length),b]
}else{
d=[...d.slice(0,f),b,...d.slice(f+1)]
}
}
c--;
return d;
}, []).join('')
}
/////////////////////////////////////////////////
s="put your text here";
/////////////////////////////////////////////////
u=encode(s);
c=`(${decode.toString().replace(/;\s*\}/g, '}').replace(/\s+/g, ' ').replace(/\s+([\.\[\]\{\}=])/g,'$1').replace(/([;,=\.\[\]\{\}=])\s+/g,'$1')})(${JSON.stringify(u)})`;
// safety check
decoded=eval(c); if (s!==decoded) { throw new Error(JSON.stringify({decodeFuncError:{decodedTo:decoded,expected:s}})); }
console.log(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment