commit fab7cbe1bd23163c5b15a424d2932a38e8f17c44
parent 4bc1557128e792096f776e9007774263b68c7c74
Author: lash <dev@holbrook.no>
Date: Sun, 18 Sep 2022 17:41:30 +0000
Get result hash from remote
Diffstat:
M | index.html | | | 55 | +++++++++++++++++++++++++++++++++++++++++-------------- |
1 file changed, 41 insertions(+), 14 deletions(-)
diff --git a/index.html b/index.html
@@ -7,6 +7,7 @@ let g_remote_key_id = '(none)';
let g_local_key_id = '(none)';
let g_data_endpoint = window.location.href;
let g_counter = undefined;
+let g_current_message = undefined;
</script>
<script src="node_modules/openpgp/dist/openpgp.min.js"></script>
<script src="node_modules/jssha/dist/sha256.js"></script>
@@ -21,6 +22,10 @@ let g_counter = undefined;
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}
+
+ function msg_identifier() {
+ return 'msg' + g_counter;
+ }
async function loadSettings() {
let rs = await fetch(window.location.href + '/settings.json', {
@@ -60,6 +65,7 @@ let g_counter = undefined;
} else {
g_counter = parseInt(c);
}
+ g_current_message = g_counter;
stateChange('load remote encryption key');
let r = await fetch(settings.remote_pubkey_url);
@@ -88,14 +94,22 @@ let g_counter = undefined;
window.dispatchEvent(ev);
}
+ async function try_dispatch(s) {
+ try {
+ return await dispatch(s)
+ } catch(e) {
+ stateChange('send fail: ' + e);
+ return 'failed';
+ // on fail the msg count will be wrong in error message
+ }
+ }
+
async function dispatch(s) {
stateChange('encrypt message ' + g_counter);
- //let sb = new TextEncoder("utf-8").encode(s);
const sha_raw = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" });
sha_raw.update(s);
const digest = sha_raw.getHash("HEX");
console.log('digest', digest);
- //let digest = await crypto.subtle.digest('SHA-256', sb);
let msg = await openpgp.createMessage({
text: s,
});
@@ -104,15 +118,18 @@ let g_counter = undefined;
encryptionKeys: enc,
format: 'binary',
message: msg,
- // config: { rejectCurves: new Set() },
});
let em = await openpgp.createMessage({
- binary: m, //_str,
+ binary: m,
});
//let emb = new TextEncoder("utf-8").encode(m);
- const sha = new jsSHA("SHA-256", "UINT8ARRAY", { encoding: "UTF8" });
- sha.update(m);
- const digest_enc = sha.getHash("HEX");
+ //const sha = new jsSHA("SHA-256", "UINT8ARRAY");
+ let pfx = msg_identifier();
+ //let pfx_b = new TextEncoder("utf-8").encode(pfx);
+ //console.log('ppffx', pfx_b, m);
+ //sha.update(pfx_b);
+ //sha.update(identity);
+ //const digest_enc = sha.getHash("HEX");
stateChange('sign message ' + g_counter);
let sig = await openpgp.sign({
@@ -120,7 +137,6 @@ let g_counter = undefined;
message: em,
format: 'binary',
detached: true,
- // config: { rejectCurves: new Set() },
});
stateChange('encode request for message ' + g_counter);
@@ -130,15 +146,15 @@ let g_counter = undefined;
//let rcpt_unencrypted = buf2hex(digest);
//let rcpt = buf2hex(digest_enc);
- rcpt = digest_enc;
+ //rcpt = digest_enc;
console.debug('digest for unencrypted message:', digest);
- console.debug('digest for encrypted message:', rcpt);
+ //console.debug('digest for encrypted message:', rcpt);
sig_b = btoa(sig_str);
pub_b = btoa(pubkey_str);
stateChange('send message ' + g_counter);
- let req = await fetch(g_data_endpoint + '/msg' + g_counter, {
+ let res = await fetch(g_data_endpoint + '/' + pfx, {
method: 'PUT',
body: m,
headers: {
@@ -146,10 +162,12 @@ let g_counter = undefined;
'Authorization': 'PUBSIG pgp:' + pub_b + ':' + sig_b,
}
});
-
+
+ rcpt = res.text();
stateChange('update local state, next message is: ' + g_counter);
g_counter += 1;
localStorage.setItem('msg_count', g_counter);
+ g_current_message += 1;
stateChange('ready to send next message');
return rcpt;
@@ -158,6 +176,15 @@ let g_counter = undefined;
window.addEventListener('messagestatechange', (v) => {
console.debug('message state change:', v.detail.s);
});
+
+ //function toEndpointUrl(s) {
+ // let u = g_data_endpoint + '/' + s;
+ // return '<a href=\"' + u + '\">' + s + '</a>';
+ //}
+
+ function toEndpointUrl(s) {
+ return s;
+ }
</script>
</head>
@@ -169,7 +196,7 @@ let g_counter = undefined;
message_count: g_counter,
rcpt: ' ',
content: '',
-
+
}">
<dl>
<dt>Application:</dt>
@@ -188,7 +215,7 @@ let g_counter = undefined;
<textarea cols=72 rows=10 x-model="content" >
</textarea>
<br/>
- <button @click="r = await dispatch(content); rcpt = 'message ' + (message_count-1) + ': ' + r">sign, encrypt and send</button>
+ <button @click="r = await try_dispatch(content); rcpt = 'message ' + g_current_message + ': ' + toEndpointUrl(r);">sign, encrypt and send</button>
</div>
</body>
</html>