-
Notifications
You must be signed in to change notification settings - Fork 576
/
ctr.js
35 lines (32 loc) · 1.22 KB
/
ctr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Copyright 2014-2017 browserify-aes contributors. All rights reserved. MIT license.
// Copyright 2013 Maxwell Krohn. All rights reserved. MIT license.
// Copyright 2009-2013 Jeff Mott. All rights reserved. MIT license.
import { xor } from "../xor.ts";
import { Buffer } from "../../../../buffer.ts";
import { incr32 } from "../incr32.js";
function getBlock(self) {
const out = self._cipher.encryptBlockRaw(self._prev);
incr32(self._prev);
return out;
}
const blockSize = 16;
export const encrypt = function (self, chunk) {
const chunkNum = Math.ceil(chunk.length / blockSize);
const start = self._cache.length;
self._cache = Buffer.concat([
self._cache,
Buffer.allocUnsafe(chunkNum * blockSize),
]);
for (let i = 0; i < chunkNum; i++) {
const out = getBlock(self);
const 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);
}
const pad = self._cache.slice(0, chunk.length);
self._cache = self._cache.slice(chunk.length);
return xor(chunk, pad);
};