added: pgp encryption

This commit is contained in:
quenousimporte 2023-07-13 13:18:47 +02:00
parent 0fc50c22cf
commit d710a1f533
2 changed files with 45 additions and 7 deletions

View File

@ -18,7 +18,7 @@
<body onload="init()" onkeydown="mainkeydownhandler()" onresize="resize()" onstorage="loadstorage()"> <body onload="init()" onkeydown="mainkeydownhandler()" onresize="resize()" onstorage="loadstorage()">
<script src="libs/showdown.min.js"></script> <script src="libs/showdown.min.js"></script>
<script src="libs/vis-network.min.js"></script> <script src="libs/vis-network.min.js"></script>
<script src="libs/openpgp.min.js"></script>
<script src="main.js"></script> <script src="main.js"></script>
<div id="networkpage" hidden> <div id="networkpage" hidden>

50
main.js
View File

@ -15,7 +15,8 @@ var defaultsettings =
enablenetwork: false, enablenetwork: false,
titlebydefault: false, titlebydefault: false,
hideheaderbydefault: true, hideheaderbydefault: true,
linksinnewtab: true linksinnewtab: true,
pgp: false
}; };
//builtin //builtin
@ -1142,6 +1143,18 @@ function loadsettings()
{ {
toggletitle(); toggletitle();
} }
if (settings.pgp)
{
if (!localStorage.getItem("publickey"))
{
localStorage.setItem("publickey", prompt("Public key"));
}
if (!localStorage.getItem("privatekey"))
{
localStorage.setItem("privatekey", prompt("Private key"));
}
}
} }
function checksaved() function checksaved()
@ -1200,6 +1213,7 @@ function addfakehistory()
{ {
history.pushState({}, '', '.'); history.pushState({}, '', '.');
} }
function init() function init()
{ {
loadsettings(); loadsettings();
@ -1397,7 +1411,7 @@ function checkevents()
.catch(remotecallfailed); .catch(remotecallfailed);
} }
function queryremote(params) async function queryremote(params)
{ {
return new Promise( (apply, failed) => { return new Promise( (apply, failed) => {
@ -1424,7 +1438,7 @@ function queryremote(params)
failed("XMLHttpRequest error"); failed("XMLHttpRequest error");
} }
xhr.onload = function() xhr.onload = async function()
{ {
if (xhr.status !== 200) if (xhr.status !== 200)
{ {
@ -1435,7 +1449,21 @@ function queryremote(params)
var data = {}; var data = {};
try try
{ {
data = JSON.parse(xhr.responseText); var response = xhr.responseText;
if (settings.pgp && response.startsWith("-----BEGIN PGP MESSAGE-----"))
{
console.log("decrypting...")
var privateKey = await openpgp.readKey({ armoredKey: localStorage.getItem("privatekey") });
var decrypted = await openpgp.decrypt({
message: await openpgp.readMessage({ armoredMessage: response }),
decryptionKeys: privateKey });
const chunks = [];
for await (const chunk of decrypted.data) {
chunks.push(chunk);
}
response = chunks.join('');
}
data = JSON.parse(response);
if (data.error) if (data.error)
{ {
@ -1825,7 +1853,7 @@ function postpone()
}); });
} }
function save() async function save()
{ {
clearTimeout(workerid); clearTimeout(workerid);
@ -1872,10 +1900,20 @@ function save()
if (isremote()) if (isremote())
{ {
var datatosend = JSON.stringify(localdata);
if (settings.pgp)
{
console.log("encrypting...");
var publicKey = await openpgp.readKey({ armoredKey: localStorage.getItem("publickey") });
datatosend = await openpgp.encrypt({
message: await openpgp.createMessage({ text: datatosend }),
encryptionKeys: publicKey });
}
console.log("sending data to php server..."); console.log("sending data to php server...");
pending = true; pending = true;
queryremote({action: "push", data: JSON.stringify(localdata)}) queryremote({action: "push", data: datatosend})
.then(() => .then(() =>
{ {
console.log("...data saved on server"); console.log("...data saved on server");