gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.53 kB
JavaScript
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */
import{U32 as t}from"../../primitive/number/U32Static.js";import{incrBE as r}from"../../primitive/xtUint8Array.js";import{LengthError as e}from"../../error/LengthError.js";import{ByteWriter as i}from"../../primitive/ByteWriter.js";export class IncrBytes{constructor(t){this._iv=t}get length(){return this._iv.length}[Symbol.iterator](){const t=this._iv.slice();return{next(){const e=t.slice();return r(t),{done:!1,value:e}}}}}export class Concat32{constructor(t,r=0){this._iv=t,this._count=r}get length(){return this._iv.length+4}[Symbol.iterator](){const r=new Uint8Array(this._iv.length+4),e=i.mount(r);e.write(this._iv);let o=this._count;return{next:()=>(t.intoBytesBE(o++,e.sub(4,!0)),{done:!1,value:r.slice()})}}}export class Ctr{constructor(t,r){if(this._crypt=t,this._counter=r,r.length!=t.blockSize)throw new e(t.blockSize,"counter.length",r.length)}get blockSize(){return this._crypt.blockSize}decryptInto(t,r){const e=this._crypt.blockSize,i=r.length%e,o=r.length-i;t.set(r);let n=0;for(const r of this._counter){if(!(n<o)){if(i>0){this._crypt.encryptBlock(r);for(let e=0;e<i;e++)t[n+e]^=r[e]}break}this._crypt.encryptBlock(r);for(let i=0;i<e;i++)t[n+i]^=r[i];n+=e}}encryptInto(t,r){const e=this._crypt.blockSize,i=r.length%e,o=r.length-i;t.set(r);let n=0;for(const r of this._counter){if(!(n<o)){if(i>0){this._crypt.encryptBlock(r);for(let e=0;e<i;e++)t[n+e]^=r[e]}break}this._crypt.encryptBlock(r);for(let i=0;i<e;i++)t[n+i]^=r[i];n+=e}}encryptSize(t){return t}}