wallet-core/node_modules/browserify-aes/modes/cfb1.js

43 lines
917 B
JavaScript
Raw Normal View History

2017-12-10 21:51:33 +01:00
var Buffer = require('safe-buffer').Buffer
2017-05-03 15:35:00 +02:00
function encryptByte (self, byteParam, decrypt) {
var pad
var i = -1
var len = 8
var out = 0
var bit, value
while (++i < len) {
pad = self._cipher.encryptBlock(self._prev)
bit = (byteParam & (1 << (7 - i))) ? 0x80 : 0
value = pad[0] ^ bit
out += ((value & 0x80) >> (i % 8))
self._prev = shiftIn(self._prev, decrypt ? bit : value)
}
return out
}
2017-12-10 21:51:33 +01:00
function shiftIn (buffer, value) {
var len = buffer.length
2017-05-03 15:35:00 +02:00
var i = -1
2017-12-10 21:51:33 +01:00
var out = Buffer.allocUnsafe(buffer.length)
buffer = Buffer.concat([buffer, Buffer.from([value])])
2017-05-03 15:35:00 +02:00
while (++i < len) {
2017-12-10 21:51:33 +01:00
out[i] = buffer[i] << 1 | buffer[i + 1] >> (7)
2017-05-03 15:35:00 +02:00
}
2017-12-10 21:51:33 +01:00
2017-05-03 15:35:00 +02:00
return out
}
2017-12-10 21:51:33 +01:00
exports.encrypt = function (self, chunk, decrypt) {
var len = chunk.length
var out = Buffer.allocUnsafe(len)
2017-05-03 15:35:00 +02:00
var i = -1
2017-12-10 21:51:33 +01:00
2017-05-03 15:35:00 +02:00
while (++i < len) {
2017-12-10 21:51:33 +01:00
out[i] = encryptByte(self, chunk[i], decrypt)
2017-05-03 15:35:00 +02:00
}
2017-12-10 21:51:33 +01:00
2017-05-03 15:35:00 +02:00
return out
}