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

31 lines
908 B
JavaScript
Raw Normal View History

2017-05-03 15:35:00 +02:00
var xor = require('buffer-xor')
2017-12-10 21:51:33 +01:00
var Buffer = require('safe-buffer').Buffer
var incr32 = require('../incr32')
2017-05-03 15:35:00 +02:00
function getBlock (self) {
2017-12-10 21:51:33 +01:00
var out = self._cipher.encryptBlockRaw(self._prev)
2017-05-03 15:35:00 +02:00
incr32(self._prev)
return out
}
2017-12-10 21:51:33 +01:00
var blockSize = 16
2017-05-03 15:35:00 +02:00
exports.encrypt = function (self, chunk) {
2017-12-10 21:51:33 +01:00
var chunkNum = Math.ceil(chunk.length / blockSize)
var start = self._cache.length
self._cache = Buffer.concat([
self._cache,
Buffer.allocUnsafe(chunkNum * blockSize)
])
for (var i = 0; i < chunkNum; i++) {
var out = getBlock(self)
var offset = start + i * blockSize
self._cache.writeUInt32BE(out[0], offset + 0)
self._cache.writeUInt32BE(out[1], offset + 4)
self._cache.writeUInt32BE(out[2], offset + 8)
self._cache.writeUInt32BE(out[3], offset + 12)
2017-05-03 15:35:00 +02:00
}
var pad = self._cache.slice(0, chunk.length)
self._cache = self._cache.slice(chunk.length)
return xor(chunk, pad)
}