- commit
- 6f4fce2498cd671e1c63f653246a191392899e6c
- parent
- 259720414057453059bb4deb2066b29d13650d18
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-05-29 16:18
encrypt parameter for optional encryption
Diffstat
| M | static/crypto.js | 12 | ++++++++---- |
| M | static/main.js | 13 | +++++++------ |
2 files changed, 15 insertions, 10 deletions
diff --git a/static/crypto.js b/static/crypto.js
@@ -82,18 +82,22 @@ export async function decrypt(bytes, key) {
82 82 ));
83 83 }
84 84
85 -1 export async function encryptText(text, key) {
-1 85 export async function encodeText(text, key) {
86 86 var encoder = new TextEncoder();
87 87 var bytes = encoder.encode(text);
88 88 bytes = await compress(bytes);
89 -1 bytes = await encrypt(bytes, key);
-1 89 if (key) {
-1 90 bytes = await encrypt(bytes, key);
-1 91 }
90 92 return b64encode(bytes);
91 93 }
92 94
93 -1 export async function decryptText(string, key) {
-1 95 export async function decodeText(string, key) {
94 96 var decoder = new TextDecoder();
95 97 var bytes = b64decode(string);
96 -1 bytes = await decrypt(bytes, key);
-1 98 if (key) {
-1 99 bytes = await decrypt(bytes, key);
-1 100 }
97 101 bytes = await decompress(bytes);
98 102 return decoder.decode(bytes);
99 103 }
diff --git a/static/main.js b/static/main.js
@@ -1,15 +1,16 @@ 1 1 import * as crypto from './crypto.js'; 2 2 3 3 var params = new URLSearchParams(location.search);4 -15 -1 var password = window.prompt('Please enter the password');6 -1 var key = await crypto.getKey(password);7 -18 4 var textarea = document.querySelector('textarea'); 9 5 -1 6 if (params.has('encrypt')) { -1 7 var password = window.prompt('Please enter the password'); -1 8 var key = await crypto.getKey(password); -1 9 } -1 10 10 11 if (location.hash.length) { 11 12 try {12 -1 var plaintext = await crypto.decryptText(location.hash.substr(1), key);-1 13 var plaintext = await crypto.decodeText(location.hash.substr(1), key); 13 14 if (params.has('view')) { 14 15 document.write(plaintext); 15 16 document.close(); @@ -23,6 +24,6 @@ if (location.hash.length) { 23 24 } 24 25 25 26 textarea.addEventListener('input', async () => {26 -1 var data = await crypto.encryptText(textarea.value, key);-1 27 var data = await crypto.encodeText(textarea.value, key); 27 28 history.replaceState(null, '', `#${data}`); 28 29 });