UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 3.43 kB
/*! Copyright 2025 the gnablib contributors MPL-1.1 */ var t,o=this&&this.__classPrivateFieldSet||function(t,o,e,r,s){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!s)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof o?t!==o||!s:!o.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?s.call(t,e):s?s.value=e:o.set(t,e),e},e=this&&this.__classPrivateFieldGet||function(t,o,e,r){if("a"===e&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof o?t!==o||!r:!o.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?r:"a"===e?r.call(t):r?r.value:o.get(t)};import{asLE as r}from"../../endian/platform.js";import{U32 as s}from"../../primitive/number/U32Static.js";import{sLen as i}from"../../safe/safe.js";const l=16,n=[3287280091,1147300610,2044886154,2027892972,1902027934,3347438090,3763270186,3854829911];class a{constructor(e,r){this.blockSize=l,t.set(this,void 0),this.rounds=e,o(this,t,r,"f")}_encBlock(o){const i=new Uint32Array(o.buffer,o.byteOffset,4);r.i32(o,0,4);for(let o=0;o<this.rounds;o++){const r=i[0];i[0]=s.lRot((i[0]^e(this,t,"f")[o][0])+(i[1]^e(this,t,"f")[o][1]),9),i[1]=s.rRot((i[1]^e(this,t,"f")[o][2])+(i[2]^e(this,t,"f")[o][3]),5),i[2]=s.rRot((i[2]^e(this,t,"f")[o][4])+(i[3]^e(this,t,"f")[o][5]),3),i[3]=r}r.i32(o,0,4)}_decBlock(o){const i=new Uint32Array(o.buffer,o.byteOffset,4);r.i32(o,0,4);for(let o=this.rounds-1;o>=0;o--){const r=i.slice();i[0]=r[3],i[1]=s.rRot(r[0],9)-(i[0]^e(this,t,"f")[o][0])^e(this,t,"f")[o][1],i[2]=s.lRot(r[1],5)-(i[1]^e(this,t,"f")[o][2])^e(this,t,"f")[o][3],i[3]=s.lRot(r[2],3)-(i[2]^e(this,t,"f")[o][4])^e(this,t,"f")[o][5]}r.i32(o,0,4)}decryptBlock(t,o=0){const e=o*l;i("block",t).atLeast(e+l).throwNot(),this._decBlock(t.subarray(e,e+l))}encryptBlock(t,o=0){const e=o*l;i("block",t).atLeast(e+l).throwNot(),this._encBlock(t.subarray(e,e+l))}}t=new WeakMap;export class Lea128 extends a{constructor(t){i("key",t).exactly(16).throwNot();const o=[],e=new Uint32Array(t.buffer,t.byteOffset,t.length/4).slice();for(let t=0;t<24;t++){const r=n[t%4];e[0]=s.lRot(e[0]+s.lRot(r,t),1),e[1]=s.lRot(e[1]+s.lRot(r,t+1),3),e[2]=s.lRot(e[2]+s.lRot(r,t+2),6),e[3]=s.lRot(e[3]+s.lRot(r,t+3),11),o.push(Uint32Array.of(e[0],e[1],e[2],e[1],e[3],e[1]))}super(24,o)}}export class Lea192 extends a{constructor(t){i("key",t).exactly(24).throwNot();const o=[],e=new Uint32Array(t.buffer,t.byteOffset,t.length/4).slice();for(let t=0;t<28;t++){const r=n[t%6];e[0]=s.lRot(e[0]+s.lRot(r,t),1),e[1]=s.lRot(e[1]+s.lRot(r,t+1),3),e[2]=s.lRot(e[2]+s.lRot(r,t+2),6),e[3]=s.lRot(e[3]+s.lRot(r,t+3),11),e[4]=s.lRot(e[4]+s.lRot(r,t+4),13),e[5]=s.lRot(e[5]+s.lRot(r,t+5),17),o.push(Uint32Array.of(e[0],e[1],e[2],e[3],e[4],e[5]))}super(28,o)}}export class Lea256 extends a{constructor(t){i("key",t).exactly(32).throwNot();const o=[],e=new Uint32Array(t.buffer,t.byteOffset,t.length/4).slice();for(let t=0;t<32;t++){const r=n[t%8],i=6*t%8,l=(6*t+1)%8,a=(6*t+2)%8,c=(6*t+3)%8,f=(6*t+4)%8,h=(6*t+5)%8;e[i]=s.lRot(e[i]+s.lRot(r,t),1),e[l]=s.lRot(e[l]+s.lRot(r,t+1),3),e[a]=s.lRot(e[a]+s.lRot(r,t+2),6),e[c]=s.lRot(e[c]+s.lRot(r,t+3),11),e[f]=s.lRot(e[f]+s.lRot(r,t+4),13),e[h]=s.lRot(e[h]+s.lRot(r,t+5),17),o.push(Uint32Array.of(e[i],e[l],e[a],e[c],e[f],e[h]))}super(32,o)}}