aboutsummaryrefslogtreecommitdiff
path: root/frontend/decryptor.js
blob: c37aa42975949bf4a24c1de700df80209d35a490 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//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='';}
}