-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
97 lines (85 loc) · 2.41 KB
/
script.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
/* This file is part of Just Encrypt.
*
* Just Encrypt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Just Encrypt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Just Encrypt. If not, see <http://www.gnu.org/licenses/>.
*/
var pwField, dataField, outputDisplay, encryptRB, decryptRB, button, plainTextLink;
function encrypt() {
var pw = pwField.value;
var pwConf = pwConfField.value;
if (pw != pwConf) {
window.alert("The password and confirm password fields do not match. Try retyping the password.");
return;
}
var data = dataField.value;
var result = sjcl.encrypt(pw, data);
output(result);
}
function decrypt() {
var pw = pwField.value;
var data = dataField.value
var result;
try {
result = sjcl.decrypt(pw, data);
output(result);
} catch (e) {
if (e instanceof sjcl.exception.corrupt) {
window.alert("Incorrect password.");
} else if (e instanceof sjcl.exception.invalid) {
window.alert("The input data is not valid. It should start and end with curly braces ('{' and '}').");
}
}
}
// UI //
function output(text) {
outputDisplay.value = text;
plainTextLink.href = "data:text/plain," + text
}
function showPWConfirm() {
pwConfLabel.style.display = 'inline';
pwConfField.style.display = 'inline';
}
function hidePWConfirm() {
pwConfLabel.style.display = 'none';
pwConfField.style.display = 'none';
}
function checkPWMatch() {
var newClassName;
if (pwField.value == "" || pwConfField.value == "" || decryptRB.checked) {
newClassName = "";
} else if (pwField.value == pwConfField.value) {
newClassName = "valid";
} else {
newClassName = "invalid";
}
pwField.className = newClassName;
pwConfField.className = newClassName;
}
// Event Handlers //
function encryptRB_onclick() {
decryptRB.checked = false;
showPWConfirm();
checkPWMatch();
}
function decryptRB_onclick() {
encryptRB.checked = false;
hidePWConfirm();
checkPWMatch();
}
function button_onclick() {
if (encryptRB.checked) {
encrypt();
} else {
decrypt();
}
}