From 6d1a93b31484dfbcf38d7381cc7b3786afab49aa Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 17 Oct 2022 15:33:51 +0200 Subject: [PATCH] Decrypt page improvements --- decrypt/index.html | 78 ++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/decrypt/index.html b/decrypt/index.html index 3b55d18573..81913d52c2 100644 --- a/decrypt/index.html +++ b/decrypt/index.html @@ -30,61 +30,77 @@ window.addEventListener('load', load); function load() { - document.getElementById('getpassword').addEventListener('submit', getpassword); + let form = document.getElementById('form') + let password = document.getElementById('password'); + let message = document.getElementById('message'); + let error = document.getElementById('error'); + let details = document.getElementById('details'); + + form.addEventListener('submit', submit); + if (window.location.hash) if (crypto.subtle && typeof Uint8Array === 'function' && typeof TextEncoder === 'function') { - document.getElementById('getpassword').style.display = 'block'; - document.getElementById('password').focus(); + form.style.display = 'block'; + password.focus(); } else { - document.getElementById('error').textContent = 'Your browser is not suitable for decrypting messages'; - document.getElementById('error').style.display = 'block'; - document.getElementById('details').innerHTML = + error.textContent = 'Your browser is unsuitable for decrypting text'; + error.style.display = 'block'; + details.innerHTML = 'crypto.subtle: ' + (crypto.subtle ? 'Yes' : 'No') + '
' + 'Uint8Array: ' + (Uint8Array ? 'Yes' : 'No') + '
' + 'TextEncoder: ' + (TextEncoder ? 'Yes' : 'No') + '
'; - document.getElementById('details').style.display = 'block'; + details.style.display = 'block'; } else { - document.getElementById('message').textContent = 'Nothing to see here'; - document.getElementById('message').style.display = 'block'; + message.innerHTML = 'Nothing to see here'; + message.style.display = 'block'; } document.getElementById('year').textContent = new Date().getFullYear(); } - function getpassword(event) { + function submit(event) { event.preventDefault(); decrypt(); } async function decrypt() { - document.getElementById('fields').disabled = true; + let fields = document.getElementById('fields'); + let form = document.getElementById('form') + let password = document.getElementById('password'); + let message = document.getElementById('message'); + let error = document.getElementById('error'); + let details = document.getElementById('details'); + let copyright = document.getElementById('copyright'); + try { - document.getElementById('message').style.display = 'none'; - document.getElementById('error').style.display = 'none'; - document.getElementById('details').style.display = 'none'; - let password = document.getElementById('password').value; - if (password) { - let message = await _decrypt(password); - document.getElementById('getpassword').style.display = 'none'; - document.getElementById('message').innerHTML = message; - document.getElementById('message').style.display = 'block'; - document.getElementById('copyright').style.display = 'none'; - } - else + fields.disabled = true; + message.style.display = 'none'; + error.style.display = 'none'; + details.style.display = 'none'; + + if (!password.value) throw new Error('Password required'); + + let html = await _decrypt(password.value); + + form.style.display = 'none'; + message.innerHTML = html; + message.style.display = 'block'; + copyright.style.display = 'none'; } catch (e) { console.log("%O", e); - document.getElementById('password').value = ''; - document.getElementById('error').textContent = 'Could not decrypt the message. Is the password correct?'; - document.getElementById('error').style.display = 'block'; - document.getElementById('details').textContent = e.toString(); - document.getElementById('details').style.display = 'block'; + fields.disabled = false; + password.value = ''; + password.focus(); + error.textContent = 'Could not decrypt the message. Is the password correct?'; + error.style.display = 'block'; + details.textContent = e.toString(); + details.style.display = 'block'; } - document.getElementById('fields').disabled = false; } async function _decrypt(password) { @@ -105,7 +121,7 @@ const importedKey = await crypto.subtle.importKey('raw', passwordBuffer, 'PBKDF2', false, ['deriveBits']); const derivation = await crypto.subtle.deriveBits({name: 'PBKDF2', hash: 'SHA-512', salt: salt, iterations: 120000}, importedKey, 256); const importedEncryptionKey = await crypto.subtle.importKey('raw', derivation, {name: 'AES-GCM'}, false, ['decrypt']); - const decrypted = await window.crypto.subtle.decrypt({name: 'AES-GCM', iv: iv, tagLength: 128}, importedEncryptionKey, e); + const decrypted = await crypto.subtle.decrypt({name: 'AES-GCM', iv: iv, tagLength: 128}, importedEncryptionKey, e); return new TextDecoder('UTF-8').decode(decrypted); } @@ -114,7 +130,7 @@

Please enable JavaScript

-