You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
869 B
34 lines
869 B
4 years ago
|
var Buffer = require('safe-buffer').Buffer
|
||
|
var xor = require('buffer-xor')
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
exports.encrypt = function (self, data, decrypt) {
|
||
|
var out = Buffer.allocUnsafe(0)
|
||
|
var len
|
||
|
|
||
|
while (data.length) {
|
||
|
if (self._cache.length === 0) {
|
||
|
self._cache = self._cipher.encryptBlock(self._prev)
|
||
|
self._prev = Buffer.allocUnsafe(0)
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|