2017-12-10 21:51:33 +01:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2017-05-03 15:35:00 +02:00
|
|
|
var xor = require('buffer-xor')
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function encryptStart (self, data, decrypt) {
|
|
|
|
var len = data.length
|
|
|
|
var out = xor(data, self._cache)
|
|
|
|
self._cache = self._cache.slice(len)
|
|
|
|
self._prev = Buffer.concat([self._prev, decrypt ? data : out])
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
exports.encrypt = function (self, data, decrypt) {
|
2017-12-10 21:51:33 +01:00
|
|
|
var out = Buffer.allocUnsafe(0)
|
2017-05-03 15:35:00 +02:00
|
|
|
var len
|
|
|
|
|
|
|
|
while (data.length) {
|
|
|
|
if (self._cache.length === 0) {
|
|
|
|
self._cache = self._cipher.encryptBlock(self._prev)
|
2017-12-10 21:51:33 +01:00
|
|
|
self._prev = Buffer.allocUnsafe(0)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self._cache.length <= data.length) {
|
|
|
|
len = self._cache.length
|
|
|
|
out = Buffer.concat([out, encryptStart(self, data.slice(0, len), decrypt)])
|
|
|
|
data = data.slice(len)
|
|
|
|
} else {
|
|
|
|
out = Buffer.concat([out, encryptStart(self, data, decrypt)])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|