gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 781 B
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
export class Ecb{constructor(t,r){this._crypt=t,this._pad=r}get blockSize(){return this._crypt.blockSize}decryptInto(t,r){const c=this._crypt.blockSize,s=r.length-c;let e=0,p=0;for(t.set(r.subarray(0,s));e<s;)this._crypt.decryptBlock(t,p++),e+=c;const i=new Uint8Array(r.subarray(s));this._crypt.decryptBlock(i),t.set(this._pad.unpad(i),s)}encryptInto(t,r){const c=this._crypt.blockSize,s=r.length%c,e=r.length-s,p=e+(this._pad.padSize(0===s?c:s,c)>0?c:0);t.set(r.subarray(0,e));let i=0,o=0;for(;i<e;)this._crypt.encryptBlock(t,o++),i+=c;if(p>e){const s=this._pad.pad(r.subarray(e),c);t.set(s,i),this._crypt.encryptBlock(t,o)}}encryptSize(t){const r=this._crypt.blockSize,c=t%r;return this._pad.padSize(0===c?r:c,r)-c+t}}