1
|
var Buffer = require('safe-buffer').Buffer
|
2
|
var xor = require('buffer-xor')
|
3
|
|
4
|
function encryptStart (self, data, decrypt) {
|
5
|
var len = data.length
|
6
|
var out = xor(data, self._cache)
|
7
|
self._cache = self._cache.slice(len)
|
8
|
self._prev = Buffer.concat([self._prev, decrypt ? data : out])
|
9
|
return out
|
10
|
}
|
11
|
|
12
|
exports.encrypt = function (self, data, decrypt) {
|
13
|
var out = Buffer.allocUnsafe(0)
|
14
|
var len
|
15
|
|
16
|
while (data.length) {
|
17
|
if (self._cache.length === 0) {
|
18
|
self._cache = self._cipher.encryptBlock(self._prev)
|
19
|
self._prev = Buffer.allocUnsafe(0)
|
20
|
}
|
21
|
|
22
|
if (self._cache.length <= data.length) {
|
23
|
len = self._cache.length
|
24
|
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)])
|
25
|
data = data.slice(len)
|
26
|
} else {
|
27
|
out = Buffer.concat([out, encryptStart(self, data, decrypt)])
|
28
|
break
|
29
|
}
|
30
|
}
|
31
|
|
32
|
return out
|
33
|
}
|