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