gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 891 B
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{LengthError as t}from"../../error/LengthError.js";export class Cfb{constructor(r,e){if(e.length!=r.blockSize)throw new t(r.blockSize,"iv.length",e.length);this._crypt=r,this._iv=e}get blockSize(){return this._crypt.blockSize}decryptInto(t,r){const e=this._crypt.blockSize,c=r.length%e,o=r.length-c;t.set(r);let i=0;const s=this._iv.slice();for(;i<o;){this._crypt.encryptBlock(s);for(let r=0;r<e;r++)t[i+r]^=s[r];s.set(r.subarray(i,i+e)),i+=e}if(c>0){this._crypt.encryptBlock(s);for(let r=0;r<c;r++)t[i+r]^=s[r]}}encryptInto(t,r){const e=this._crypt.blockSize,c=r.length%e,o=r.length-c;t.set(r);let i=0;const s=this._iv.slice();for(;i<o;){this._crypt.encryptBlock(s);for(let r=0;r<e;r++)t[i+r]^=s[r];s.set(t.subarray(i,i+e)),i+=e}if(c>0){this._crypt.encryptBlock(s);for(let r=0;r<c;r++)t[i+r]^=s[r]}}encryptSize(t){return t}}