fix: buffer rotation

This commit is contained in:
Richard Ramos 2022-12-21 14:22:17 -04:00
parent facb7f4be1
commit 6299732797
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C

View File

@ -74,6 +74,16 @@ export class MessageNametagBuffer {
return true;
}
rotateLeft(k: number): void {
if (k < 0 || this.buffer.length == 0) {
return;
}
const idx = this.buffer.length - (k % this.buffer.length);
const a1 = this.buffer.slice(idx);
const a2 = this.buffer.slice(0, idx);
this.buffer = a1.concat(a2);
}
// Deletes the first n elements in buffer and appends n new ones
delete(n: number): void {
if (n <= 0) {
@ -87,10 +97,7 @@ export class MessageNametagBuffer {
// Note that if the input MessageNametagBuffer is set to default, nothing is done here
if (this.secret) {
// We rotate left the array by n
for (let i = 0; i < n; i++) {
const first = this.buffer.shift();
if (first) this.buffer.push(first);
}
this.rotateLeft(n);
for (let i = 0; i < n; i++) {
const counterBytesLE = writeUIntLE(new Uint8Array(8), this.counter, 0, 8);