-
Notifications
You must be signed in to change notification settings - Fork 164
/
decrypt.js
121 lines (96 loc) · 3.24 KB
/
decrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Created by Jacob Strieb
* May 2020
*/
/*******************************************************************************
* Helper Functions
******************************************************************************/
// Highlight the text in an input with a given id
function highlight(id) {
let output = document.querySelector("#" + id);
output.focus();
output.select()
output.setSelectionRange(0, output.value.length + 1);
return output;
}
// Display a message in the "alert" area
function error(text) {
const alertText = document.querySelector(".alert");
alertText.innerText = text;
alertText.style.opacity = 1;
}
/*******************************************************************************
* Main UI Functions
******************************************************************************/
async function onDecrypt() {
// Fail if the b64 library or API was not loaded
if (!("b64" in window && "apiVersions" in window)) {
error("Important libraries not loaded!");
return;
}
// Try to get page data from the URL if possible
const urlText = document.querySelector("#encrypted-url").value;
let url;
try {
url = new URL(urlText);
} catch {
error("Entered text is not a valid URL. Make sure it includes \"https://\" too!");
return;
}
let params;
try {
params = JSON.parse(b64.decode(url.hash.slice(1)));
} catch {
error("The link appears corrupted.");
return;
}
// Check that all required parameters encoded in the URL are present
if (!("v" in params && "e" in params)) {
error("The link appears corrupted. The encoded URL is missing necessary parameters.");
return;
}
// Check that the version in the parameters is valid
if (!(params["v"] in apiVersions)) {
error("Unsupported API version. The link may be corrupted.");
return;
}
const api = apiVersions[params["v"]];
// Get values for decryption
const encrypted = b64.base64ToBinary(params["e"]);
const salt = "s" in params ? b64.base64ToBinary(params["s"]) : null;
const iv = "i" in params ? b64.base64ToBinary(params["i"]) : null;
const password = document.querySelector("#password").value;
// Decrypt if possible
let decrypted;
try {
decrypted = await api.decrypt(encrypted, password, salt, iv);
} catch {
error("Incorrect password!");
return;
}
// Print the decrypted link to the output area
document.querySelector("#output").value = decrypted;
error("Decrypted!");
// Update the "Open in New Tab" button to link to the correct place
document.querySelector("#open").href = decrypted;
}
// Activated when the "Copy" button is pressed
function onCopy(id) {
// Select and copy
const output = highlight(id);
document.execCommand("copy");
// Alert the user that the text was successfully copied
const alertArea = document.querySelector("#copy-alert");
alertArea.innerText = `Copied ${output.value.length} characters`;
alertArea.style.opacity = "1";
setTimeout(() => { alertArea.style.opacity = 0; }, 3000);
// Deselect
output.selectionEnd = output.selectionStart;
output.blur();
}
function main() {
if (window.location.hash) {
document.querySelector("#encrypted-url").value =
`https://jstrieb.github.io/link-lock/${window.location.hash}`;
}
}