summaryrefslogblamecommitdiff
path: root/blog/decryptor.js
blob: c37aa42975949bf4a24c1de700df80209d35a490 (plain) (tree)



















































































                                                                                                                                     
//License: Expat(MIT)
//Chrisoft Xiong 2017-2018
var decid;
function decryptui(id)
{
	document.getElementById("decryptui").style.display="block";
	document.getElementById("keyinp").focus();
	setTimeout(function(){document.getElementById("decryptui").style.opacity="1";},20);
	decid=id;
	document.getElementById("keyhint").innerHTML="Hint: "+document.querySelector(`encrypted[enid="${id}"]`).getAttribute("hint");
	document.getElementById("keyinp").onkeypress=function(e){if(e.keyCode==13)document.getElementById('btndecrypt').click();}
}
function hidedecryptui()
{
	document.getElementById("decryptui").style.opacity="0";
	setTimeout(function(){
		document.getElementById("decryptui").style.display="none";
		document.getElementById("keyinp").value="";
	},500);
}
function _decrypt(id,ctr)
{
	const aes=aesjs,sha256=Sha256;
	const e=document.querySelector(`encrypted[enid="${id}"]`);
	const cont=e.getAttribute("encont");
	const bc=atob(cont);
	const b=new Array(bc.length);
	for(var i=0;i<bc.length;++i)b[i]=bc.charCodeAt(i);
	const deccont=ctr.decrypt(b);
	const hash=sha256.hash(aes.utils.hex.fromBytes(deccont),{msgFormat:'hex-bytes'})
	if(hash!=e.getAttribute("hash"))return false;
	e.innerHTML=aes.utils.utf8.fromBytes(deccont);
	e.removeAttribute("encont");
	return true;
}
function _memokey(id,enk)
{
	try
	{
		const os=localStorage.getItem(`blogkeys${window.location.pathname}`);
		const o=os&&os.length?JSON.parse(os):[];
		o[Number(id)]=enk;
		localStorage.setItem(`blogkeys${window.location.pathname}`,JSON.stringify(o));
	}catch(e){console.warn(`local storage failed: ${e}`);}
}
async function decryptor(id,key)
{
	const aes=aesjs,sha256=Sha256;
	const keyu8=aes.utils.utf8.toBytes(document.getElementById("keyinp").value);
	const saltu8=aes.utils.utf8.toBytes('hellwhymustiaddsalttothiscrap');
	const enckey=await new Promise(
		(resolv,rej)=>
		{scrypt(keyu8,saltu8,1024,16,2,32,(e,p,k)=>{e?rej(e):k?resolv(k):undefined;});}
	);
	const ctr=new aes.ModeOfOperation.ctr(enckey);
	if(!_decrypt(id,ctr))
	{
		alert("The decryption key you have entered could be wrong, please try again.");
		return;
	}
	_memokey(id,enckey);
	for(let i of document.querySelectorAll('encrypted[encont]'))
	if(_decrypt(i.getAttribute('enid'),ctr))_memokey(i.getAttribute('enid'),enckey);
	footnoter();document.getElementById('purgep').style.display='';
	hidedecryptui();
}
function _purgep()
{
	try{localStorage.removeItem(`blogkeys${window.location.pathname}`);}
	catch(e){}
	location.reload(true);
}
function _decryptonload()
{
	let o;
	try
	{
		const os=localStorage.getItem(`blogkeys${window.location.pathname}`);
		o=os&&os.length?JSON.parse(os):[];
	}catch(e){console.warn(`local storage failed: ${e}`);}
	for(let i of o.entries())
	if(i[1]&&i[1].length)_decrypt(Number(i[0]).toString(),new aesjs.ModeOfOperation.ctr(i[1]));
	if(o.length){footnoter();document.getElementById('purgep').style.display='';}
}