-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
258 lines (221 loc) · 6.6 KB
/
index.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* eslint-env browser */
const nacl = require('nacl-stream')
const Nanobus = require('nanobus')
function checkArgs (key, nonce, maxChunkLength) {
if (key.length !== 32) return Error('bad key length, must be 32 bytes')
if (nonce.length !== 16) return new Error('bad nonce length, must be 16 bytes')
if (maxChunkLength >= 0xffffffff) return new Error('max chunk length is too large')
if (maxChunkLength < 16) return new Error('max chunk length is too small')
}
function encrypt (key, nonce, blob, opts, cb) {
if (!opts) opts = {}
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = Object.assign({
chunkSize: 1024 * 1024,
mimeType: blob.type,
name: blob.name
}, opts) // defaults
const argsNotOk = checkArgs(key, nonce, opts.chunkSize)
if (argsNotOk) return cb(argsNotOk)
const bus = new Nanobus()
const encryptedChunks = []
let position = 0
const worker = new Worker('./encrypt-worker.js')
worker.onmessage = function (ev) {
switch (ev.data.name) {
case 'ENCRYPT_START_OK': {
return postNextChunk()
}
case 'ENCRYPT_CHUNK_OK': {
encryptedChunks.push(ev.data.encryptedChunk)
bus.emit('progress', { position, length: blob.size })
if (!ev.data.isLast) {
return postNextChunk()
}
return worker.postMessage({
name: 'ENCRYPT_FINISH'
})
}
case 'ENCRYPT_FINISH_OK': {
if (opts.name) {
return cb(null, new File(encryptedChunks, opts.name, { type: opts.mimeType }))
} else {
return cb(null, new Blob(encryptedChunks, { type: opts.mimeType }))
}
}
case 'ENCRYPT_CANCEL_OK': {
return cb(ev.data.reason)
}
default: {
throw new Error('received unknown message from worker ' + event.data.name)
}
}
}
function error (reason) {
worker.postMessage({
name: 'ENCRYPT_CANCEL',
reason: reason
})
}
// Reads blob slice contents as Uint8Array, passing it to callback.
function readBlobSlice (blob, start, end, callback) {
var reader = new FileReader() // XXX cache reader as the enclosed function's var?
reader.onerror = function (ev) {
error(ev) // TODO where is actual error string?
}
reader.onload = function () {
callback(new Uint8Array(reader.result))
}
reader.readAsArrayBuffer(blob.slice(start, end))
}
// Feeds next chunk to worker and advances position.
function postNextChunk () {
var isLast = false
var end = position + opts.chunkSize
if (end >= blob.size) {
end = blob.size
isLast = true
}
readBlobSlice(blob, position, end, function (chunk) {
worker.postMessage({
name: 'ENCRYPT_CHUNK',
chunk: chunk,
isLast: isLast
})
// Advance position.
position = end
})
}
// Start encryption!
worker.postMessage({
name: 'ENCRYPT_START',
key: key,
nonce: nonce,
maxChunkLength: opts.chunkSize
})
return bus
}
exports.encrypt = encrypt
function decrypt (key, nonce, blob, opts, cb) {
if (!opts) opts = {}
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = Object.assign({
chunkSize: 1024 * 1024,
mimeType: blob.type,
name: blob.name
}, opts) // defaults
const argsNotOk = checkArgs(key, nonce, opts.chunkSize)
if (argsNotOk) return cb(argsNotOk)
const bus = new Nanobus()
const decryptedChunks = []
let position = 0
let nextChunkSize = -1
const worker = new Worker('./decrypt-worker.js')
worker.onmessage = function (ev) {
switch (ev.data.name) {
case 'DECRYPT_START_OK': {
return postNextChunk()
}
case 'DECRYPT_CHUNK_OK': {
if (!ev.data.decryptedChunk) {
return error('decryption failed')
}
decryptedChunks.push(ev.data.decryptedChunk)
bus.emit('progress', { position, length: blob.size })
if (!ev.data.isLast) {
return postNextChunk()
}
return worker.postMessage({
name: 'DECRYPT_FINISH'
})
}
case 'DECRYPT_FINISH_OK': {
if (opts.name) {
return cb(null, new File(decryptedChunks, opts.name, { type: opts.mimeType }))
} else {
return cb(null, new Blob(decryptedChunks, { type: opts.mimeType }))
}
}
case 'DECRYPT_CANCEL_OK': {
return cb(ev.data.reason)
}
default: {
throw new Error('received unknown message from worker ' + ev.data.name)
}
}
}
// Cancel decryption with error.
function error (reason) {
worker.postMessage({
name: 'DECRYPT_CANCEL',
reason: reason
})
}
// Reads blob slice contents as Uint8Array, passing it to callback.
function readBlobSlice (blob, start, end, callback) {
var reader = new FileReader() // XXX cache reader as the enclosed function's var?
reader.onerror = function (event) {
error(event) // XXX what's the actual error description?
}
reader.onload = function () {
callback(new Uint8Array(reader.result))
}
reader.readAsArrayBuffer(blob.slice(start, end))
}
// Feeds next chunk to worker and advances position.
function postNextChunk () {
if (nextChunkSize === -1) {
// We are just starting, so read first chunk length.
if (position + 2 >= blob.size) {
return error('blob is too short')
}
readBlobSlice(blob, position, position + 4, function (data) {
nextChunkSize = nacl.stream.readChunkLength(data)
position = 4
// Now that we have chunk size, call ourselves again.
postNextChunk()
})
} else {
// Read next chunk + length of the following chunk after it.
var isLast = false
var end = position + nextChunkSize + 16 /* tag */ + 4 /* length */
if (end >= blob.size) {
end = blob.size
isLast = true
}
readBlobSlice(blob, position - 4 /* include chunk length */, end, function (chunk) {
if (!isLast) {
// Read next chunk's length.
nextChunkSize = nacl.stream.readChunkLength(chunk, chunk.length - 4)
// Slice the length off.
chunk = chunk.subarray(0, chunk.length - 4)
} else {
nextChunkSize = 0
}
// Decrypt.
worker.postMessage({
name: 'DECRYPT_CHUNK',
chunk: chunk,
isLast: isLast
})
// Advance position.
position = end
})
}
}
// Start decryption!
worker.postMessage({
name: 'DECRYPT_START',
key: key,
nonce: nonce,
maxChunkLength: opts.chunkSize
})
return bus
}
exports.decrypt = decrypt