| skipped 45 lines |
46 | 46 | | } |
47 | 47 | | |
48 | 48 | | |
| 49 | + | // Perform encryption based on parameters, and return a base64-encoded JSON |
| 50 | + | // object containing all of the relevant data for use in the URL fragment. |
| 51 | + | async function generateFragment(url, passwd, hint, useRandomSalt, useRandomIv) { |
| 52 | + | const api = apiVersions[LATEST_API_VERSION]; |
| 53 | + | |
| 54 | + | const salt = useRandomSalt ? await api.randomSalt() : null; |
| 55 | + | const iv = useRandomIv ? await api.randomIv() : null; |
| 56 | + | const encrypted = await api.encrypt(url, passwd, salt, iv); |
| 57 | + | const output = { |
| 58 | + | v: LATEST_API_VERSION, |
| 59 | + | e: b64.binaryToBase64(new Uint8Array(encrypted)) |
| 60 | + | } |
| 61 | + | |
| 62 | + | // Add the hint if there is one |
| 63 | + | if (hint != "") { |
| 64 | + | output["h"] = hint; |
| 65 | + | } |
| 66 | + | |
| 67 | + | // Add the salt and/or initialization vector if randomly generated |
| 68 | + | if (useRandomSalt) { |
| 69 | + | output["s"] = b64.binaryToBase64(salt); |
| 70 | + | } |
| 71 | + | if (useRandomIv) { |
| 72 | + | output["i"] = b64.binaryToBase64(iv); |
| 73 | + | } |
| 74 | + | |
| 75 | + | // Return the base64-encoded output |
| 76 | + | return b64.encode(JSON.stringify(output)); |
| 77 | + | } |
| 78 | + | |
| 79 | + | |
49 | 80 | | |
50 | 81 | | /******************************************************************************* |
51 | 82 | | * Main UI Functions |
| skipped 4 lines |
56 | 87 | | let label = document.querySelector("#advanced-label"); |
57 | 88 | | let advanced = document.querySelector(".advanced"); |
58 | 89 | | if (advanced.style.display == "none" || advanced.style.display == "") { |
| 90 | + | // Note: innerHTML used instead of innerText so that the entity could be |
| 91 | + | // used rather than having to literally put the unicode in. Same below. |
59 | 92 | | label.innerHTML = "▾ advanced"; |
60 | 93 | | advanced.style.display = "flex"; |
61 | 94 | | } else { |
| skipped 4 lines |
66 | 99 | | |
67 | 100 | | |
68 | 101 | | // Activated when the "Encrypt" button is pressed |
69 | | - | function onEncrypt() { |
| 102 | + | async function onEncrypt() { |
70 | 103 | | if (!validateInputs()) { |
71 | 104 | | return; |
72 | 105 | | } |
| skipped 10 lines |
83 | 116 | | |
84 | 117 | | // Initialize values for encryption |
85 | 118 | | const url = document.querySelector("#url").value; |
86 | | - | // TODO: Finish this |
| 119 | + | const useRandomIv = document.querySelector("#iv").checked; |
| 120 | + | const useRandomSalt = document.querySelector("#salt").checked; |
87 | 121 | | |
88 | | - | const output = "foobar"; |
| 122 | + | const hint = document.querySelector("#hint").value |
| 123 | + | |
| 124 | + | const encrypted = await generateFragment(url, password, hint, useRandomSalt, |
| 125 | + | useRandomIv); |
| 126 | + | const output = `https://jstrieb.github.io/link-lock/#${encrypted}`; |
| 127 | + | |
89 | 128 | | document.querySelector("#output").value = output; |
90 | 129 | | highlight("output"); |
| 130 | + | |
| 131 | + | // Adjust "Open in New Tab" link |
| 132 | + | document.querySelector("#open").href = output; |
91 | 133 | | } |
92 | 134 | | |
93 | 135 | | |
| skipped 17 lines |