-
Notifications
You must be signed in to change notification settings - Fork 1
/
safebox.js
199 lines (172 loc) · 5.04 KB
/
safebox.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
var FileCollection = new Mongo.Collection('files');
var scrypt;
/**
* Derives a key using scrypt.
* @param {string} passphrase
* @param {Uint8Array} salt
* @return {Uint8Array}
*/
function deriveKey(passphrase, salt) {
if (!scrypt || !salt) {
return null;
}
// Compute a 16-byte scrypt key with L=2^16, r =8, p=1
return scrypt.crypto_scrypt(scrypt.encode_utf8(passphrase),
salt, 65536, 8, 1, 16);
}
/**
* Encrypts a file using the given key.
* @param {!ArrayBuffer} plaintext The file contents.
* @param {!Uint8Array} key The 128-bit encryption key
* @param {!Uint8Array} iv The initialization vector
* @return {Promise}
*/
function encryptFile(plaintext, key, iv) {
var algorithm = {
name: 'aes-gcm',
iv: iv,
additionalData: new Uint8Array(),
tagLength: 128
};
var extractable = false;
var usages = ['encrypt', 'decrypt'];
// (1) Import the key
return crypto.subtle.importKey(
'raw', key, algorithm, extractable, usages).then(function(result) {
// (2) Encrypt.
return crypto.subtle.encrypt(algorithm, result, plaintext);
});
}
/**
* Decrypts some data returning the given key.
* @param {!Uint8Array} plaintext The file contents.
* @param {!Uint8Array} key The 128-bit encryption key
* @param {!Uint8Array} iv The initialization vector
* @return {Promise}
*/
function decryptFile(ciphertext, key, iv) {
var algorithm = {
name: 'aes-gcm',
iv: iv,
additionalData: new Uint8Array(),
tagLength: 128
};
var extractable = false;
var usages = ['encrypt', 'decrypt'];
// (1) Import the key
return crypto.subtle.importKey(
'raw', key, algorithm, extractable, usages).then(function(result) {
// (2) Decrypt.
return crypto.subtle.decrypt(algorithm, result, ciphertext);
});
}
/**
* Reads and processes a file
* @param {!File} file
* @param {!Uint8Array} key
* @param {!Uint8Array} salt
*/
function readAndProcessFile(file, key, salt) {
var reader = new FileReader();
reader.onload = function() {
// Use NIST-recommended IV length of 96 bits
var iv = window.crypto.getRandomValues(new Uint8Array(12));
encryptFile(reader.result, key, iv).then(function(result) {
// Note result is an arraybuffer
FileCollection.insert({
name: file.name,
ciphertext: new Uint8Array(result),
iv: iv,
salt: salt,
createdAt: new Date()
});
}).catch(function(reason) {
showError('Could not encrypt file.');
});
};
reader.readAsArrayBuffer(file);
}
/**
* Shows an error at the top of the page or clears existing errors.
* @param {string=} opt_error the message to show.
* if undefined, clears the error div.
*/
function showError(opt_error) {
error = opt_error ? opt_error : '';
$('#error-div')[0].textContent = error;
}
if (Meteor.isClient) {
scrypt = scrypt_module_factory(0x6000000);
Template.body.events({
'click button': function (event) {
showError();
var selectedFiles = $('#files')[0].files;
if (selectedFiles.length === 0) {
showError('Please select at least one file.');
return;
}
var passphrase = window.prompt('Enter file passphrase:');
if (passphrase === null) {
return;
}
var salt = window.crypto.getRandomValues(new Uint8Array(10));
var key = deriveKey(passphrase, salt);
if (key === null) {
return;
}
for (var i = 0; i < selectedFiles.length; i++) {
readAndProcessFile(selectedFiles[i], key, salt);
}
},
'click .file-link': function(event) {
showError();
var id = event.target.id;
if (!id) {
showError('Got invalid file link.');
return;
}
var item = FileCollection.findOne(id);
if (!item) {
showError('No item matches the given ID');
return;
}
var passphrase = window.prompt('Enter file passphrase:');
if (passphrase === null) {
// User clicked 'cancel'
return;
}
var key = deriveKey(passphrase, item.salt);
if (key === null) {
showError('Error deriving encryption key!');
return;
}
decryptFile(item.ciphertext, key, item.iv).then(function(result) {
var blob = new Blob([result],
{type: 'application/octet-stream'});
var url = URL.createObjectURL(blob);
var downloadLink = $('ul.' + id);
var linkElement = downloadLink.find('a');
linkElement.attr('href', url);
if (window.chrome) {
// Setting 'download' prevents downloading in iframe in Firefox
// TODO: Is this a Firefox bug??
linkElement.attr('download', item.name);
}
linkElement.attr('target', '_blank');
downloadLink.show();
}).catch(function(reason) {
showError('Could not decrypt file.');
});
}
});
Template.body.helpers({
fileViews: function() {
return FileCollection.find({});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}