-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
220 lines (196 loc) · 9.48 KB
/
javascript.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(function () {
// Read a binary gpg encrypted file to an openpgp Message.
//
// This should probably be part of the openpgp library. Currently they have a function for reading armored text
// (Message#readArmored) but we want to be able to use gpg files (too). This is a subset of the readArmored function
// excluding the dearmor part.
//
// TODO: Open an issue or pull request on them to support this
var readBinaryMessage = function (binaryData) {
var packetlist = new openpgp.packet.List();
packetlist.read(binaryData);
return new openpgp.message.Message(packetlist);
};
// Read a binary gpg private keyring file to one or more openpgp Keys.
//
// This should probably be part of the openpgp library. Currently they have a function for reading armored text
// (Key#readArmored) but we want to be able to use gpg files (too). This is a subset of the readArmored function
// excluding the dearmor part.
//
// TODO: Open an issue or pull request on them to support this
var readBinaryKey = function (binaryData) {
var result = {};
result.keys = [];
try {
var packetlist = new openpgp.packet.List();
packetlist.read(binaryData);
var keyIndex = packetlist.indexOfTag(openpgp.enums.packet.publicKey, openpgp.enums.packet.secretKey);
if (keyIndex.length === 0) {
throw new Error('No key packet found in armored text');
}
for (var i = 0; i < keyIndex.length; i++) {
var oneKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]);
try {
var newKey = new openpgp.key.Key(oneKeyList);
result.keys.push(newKey);
} catch (e) {
result.err = result.err || [];
result.err.push(e);
}
}
} catch (e) {
result.err = result.err || [];
result.err.push(e);
}
return result;
};
// Page elements
var privateKeyDropArea = document.getElementById('private_key_drop_area'),
privateKeyFileInput = document.getElementById('private_key_file_input'),
keyPasswordArea = document.getElementById('key_password_area'),
keyPasswordInput = document.getElementById('key_password'),
privateKeyOkNotification = document.getElementById('private_key_ok_notification'),
privateKeyFilename = document.getElementById('private_key_filename'),
privateKeyErrorNotification = document.getElementById('private_key_error_notification'),
encryptedFileDropArea = document.getElementById('encrypted_file_drop_area'),
encryptedFileFileInput = document.getElementById('encrypted_file_file_input'),
encryptedFileOkNotification = document.getElementById('encrypted_file_ok_notification'),
encryptedFileFilename = document.getElementById('encrypted_file_filename'),
encryptedFileErrorNotification = document.getElementById('encrypted_file_error_notification'),
decryptingInProgress = document.getElementById('decrypting_in_progress'),
decryptedPasswordArea = document.getElementById('decrypted_password_area'),
decryptedPasswordInput = document.getElementById('decrypted_password'),
decryptedDataArea = document.getElementById('decrypted_data_area'),
decryptedDataTextarea = document.getElementById('decrypted_data');
// Local shared variables
var privateKeyFileReader = new FileReader(),
encryptedFileReader = new FileReader(),
loadedPrivateKey,
loadedEncryptedFile;
var decryptIfReady = function () {
decryptedPasswordInput.value = '';
decryptedDataTextarea.value = '';
decryptedPasswordArea.style.display = 'none';
decryptedDataArea.style.display = 'none';
if (loadedPrivateKey && loadedPrivateKey.primaryKey.isDecrypted && loadedEncryptedFile) {
decryptingInProgress.style.display = 'block';
// Wrap the slow decryption process in a timeout block so it won't block the browser,
// also give it a few milliseconds for the renderings above to happen in the browser.
// The async worker API would be useful here but it cannot work with the file:// protocol due to browser
// security restrictions and working with file:// is a hard requirement.
setTimeout(function () {
var decryptedData = openpgp.decryptMessage(loadedPrivateKey, loadedEncryptedFile),
decryptedPassword = decryptedData.split("\n")[0];
decryptingInProgress.style.display = 'none';
if (String(decryptedPassword).replace(/^\s+|\s+$/g, '') !== '') {
decryptedPasswordInput.value = decryptedPassword;
decryptedPasswordArea.style.display = 'block';
decryptedPasswordInput.focus();
if ('select' in decryptedPasswordInput) {
decryptedPasswordInput.select();
} else if ('setSelectionRange' in decryptedPasswordInput) {
decryptedPasswordInput.setSelectionRange(0, decryptedPasswordInput.value.length);
}
if (String(decryptedData).replace(/^\s+|\s+$/g, '') !== decryptedPassword) {
decryptedDataTextarea.value = decryptedData;
decryptedDataArea.style.display = 'block';
}
} else {
decryptedDataTextarea.value = decryptedData;
decryptedDataArea.style.display = 'block';
}
}, 10);
}
};
privateKeyDropArea.addEventListener('dragover', function (event) {
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
});
encryptedFileDropArea.addEventListener('dragover', function (event) {
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
});
privateKeyFileReader.onload = function (event) {
// TODO: handle more than one keys in the keyfile
loadedPrivateKey = readBinaryKey(event.target.result).keys[0] ||
openpgp.key.readArmored(event.target.result).keys[0];
if (loadedPrivateKey && loadedPrivateKey.isPrivate() && loadedPrivateKey.primaryKey) {
if (loadedPrivateKey.primaryKey.isDecrypted) {
keyPasswordArea.style.display = 'none';
privateKeyOkNotification.style.display = 'block';
} else {
keyPasswordArea.style.display = 'block';
privateKeyOkNotification.style.display = 'none';
keyPasswordInput.focus();
}
} else {
privateKeyErrorNotification.style.display = 'block';
}
decryptIfReady();
};
var handlePrivateKeyFile = function (file) {
loadedPrivateKey = null;
keyPasswordArea.style.display = 'none';
privateKeyOkNotification.style.display = 'none';
privateKeyErrorNotification.style.display = 'none';
privateKeyFilename.textContent = file.name;
privateKeyFileReader.readAsBinaryString(file);
};
privateKeyFileInput.addEventListener('drop', function (event) {
event.stopPropagation();
});
privateKeyFileInput.addEventListener('change', function (event) {
// TODO: handle multiple key file drops
handlePrivateKeyFile(event.target.files[0]);
});
privateKeyDropArea.addEventListener('drop', function (event) {
event.preventDefault();
// TODO: handle multiple key file drops
handlePrivateKeyFile(event.dataTransfer.files[0]);
});
keyPasswordInput.addEventListener('keydown', function (event) {
if (event.keyCode === 13) {
loadedPrivateKey.decrypt(keyPasswordInput.value);
keyPasswordInput.value = '';
if (loadedPrivateKey.primaryKey.isDecrypted) {
keyPasswordArea.style.display = 'none';
privateKeyOkNotification.style.display = 'block';
}
decryptIfReady();
}
});
encryptedFileReader.onload = function (event) {
try {
loadedEncryptedFile = readBinaryMessage(event.target.result);
} catch (e) {
try {
loadedEncryptedFile = openpgp.message.readArmored(event.target.result);
} catch (e) {
encryptedFileErrorNotification.style.display = 'block';
}
}
if (loadedEncryptedFile) {
encryptedFileOkNotification.style.display = 'block';
}
decryptIfReady();
};
var handleEncryptedFileFile = function (file) {
loadedEncryptedFile = null;
encryptedFileOkNotification.style.display = 'none';
encryptedFileErrorNotification.style.display = 'none';
encryptedFileFilename.textContent = file.name;
encryptedFileReader.readAsBinaryString(file);
};
encryptedFileFileInput.addEventListener('drop', function (event) {
event.stopPropagation();
});
encryptedFileFileInput.addEventListener('change', function (event) {
// TODO: handle multiple encrypted file drops
handleEncryptedFileFile(event.target.files[0]);
});
encryptedFileDropArea.addEventListener('drop', function (event) {
event.preventDefault();
// TODO: handle multiple encrypted file drops
handleEncryptedFileFile(event.dataTransfer.files[0]);
});
}());