1
|
var Buffer = require('safe-buffer').Buffer
|
2
|
|
3
|
function encryptByte (self, byteParam, decrypt) {
|
4
|
var pad
|
5
|
var i = -1
|
6
|
var len = 8
|
7
|
var out = 0
|
8
|
var bit, value
|
9
|
while (++i < len) {
|
10
|
pad = self._cipher.encryptBlock(self._prev)
|
11
|
bit = (byteParam & (1 << (7 - i))) ? 0x80 : 0
|
12
|
value = pad[0] ^ bit
|
13
|
out += ((value & 0x80) >> (i % 8))
|
14
|
self._prev = shiftIn(self._prev, decrypt ? bit : value)
|
15
|
}
|
16
|
return out
|
17
|
}
|
18
|
|
19
|
function shiftIn (buffer, value) {
|
20
|
var len = buffer.length
|
21
|
var i = -1
|
22
|
var out = Buffer.allocUnsafe(buffer.length)
|
23
|
buffer = Buffer.concat([buffer, Buffer.from([value])])
|
24
|
|
25
|
while (++i < len) {
|
26
|
out[i] = buffer[i] << 1 | buffer[i + 1] >> (7)
|
27
|
}
|
28
|
|
29
|
return out
|
30
|
}
|
31
|
|
32
|
exports.encrypt = function (self, chunk, decrypt) {
|
33
|
var len = chunk.length
|
34
|
var out = Buffer.allocUnsafe(len)
|
35
|
var i = -1
|
36
|
|
37
|
while (++i < len) {
|
38
|
out[i] = encryptByte(self, chunk[i], decrypt)
|
39
|
}
|
40
|
|
41
|
return out
|
42
|
}
|