1
|
var xor = require('buffer-xor')
|
2
|
var Buffer = require('safe-buffer').Buffer
|
3
|
var incr32 = require('../incr32')
|
4
|
|
5
|
function getBlock (self) {
|
6
|
var out = self._cipher.encryptBlockRaw(self._prev)
|
7
|
incr32(self._prev)
|
8
|
return out
|
9
|
}
|
10
|
|
11
|
var blockSize = 16
|
12
|
exports.encrypt = function (self, chunk) {
|
13
|
var chunkNum = Math.ceil(chunk.length / blockSize)
|
14
|
var start = self._cache.length
|
15
|
self._cache = Buffer.concat([
|
16
|
self._cache,
|
17
|
Buffer.allocUnsafe(chunkNum * blockSize)
|
18
|
])
|
19
|
for (var i = 0; i < chunkNum; i++) {
|
20
|
var out = getBlock(self)
|
21
|
var offset = start + i * blockSize
|
22
|
self._cache.writeUInt32BE(out[0], offset + 0)
|
23
|
self._cache.writeUInt32BE(out[1], offset + 4)
|
24
|
self._cache.writeUInt32BE(out[2], offset + 8)
|
25
|
self._cache.writeUInt32BE(out[3], offset + 12)
|
26
|
}
|
27
|
var pad = self._cache.slice(0, chunk.length)
|
28
|
self._cache = self._cache.slice(chunk.length)
|
29
|
return xor(chunk, pad)
|
30
|
}
|