gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 4.49 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
import{U64 as t,U64MutArray as s}from"../../primitive/number/U64.js";import{sInt as e,sLen as i}from"../../safe/safe.js";import{Key as o,Threefish1024 as r,Threefish256 as h,Threefish512 as a,Tweak as n}from"../sym/Threefish.js";const c=new Uint8Array(0);class k{constructor(t,e,i,r,h){this._hasData=!1,this._bPos=0,this.blockSize=t,this.size=e,this._block=new Uint8Array(t),this._b64=s.mount(new Uint32Array(this._block.buffer)),this._hVal=o.fromSize(t),this._tweak=n.NewKey(),this._tfConstructor=i,this._c=new i(this._hVal,this._tweak),r.length>0&&(this.write(r),this.finalize(),this._hVal.lock=!1,this._block.fill(0)),this._tweak.makeCfg(32),this._block[0]=83,this._block[1]=72,this._block[2]=65,this._block[3]=51,this._block[4]=1,this._block[8]=e<<3,this._block[9]=e>>>5,this._block[10]=e>>>13,this._block[11]=e>>>21,this.ubi(),this._bPos=0,h.length>0&&(this._tweak.makeKdf(),this.write(h),this.finalize()),this._cfgBackup=this._hVal.k64.clone(),this._tweak.makeMsg(),this._hasData=!1}ubi(){const t=this._b64.clone();this._c.encryptBlock(this._block),this._hVal.k64.set(t);const s=this._hVal.k64.length-1;for(let t=0;t<s;t++)this._hVal.k64.at(t).xorEq(this._b64.at(t));this._hVal.lock=!1}hash(){this._tweak.incr(this.blockSize),this.ubi(),this._tweak.isFirst=!1,this._bPos=0}finalize(){this._tweak.incr(this._bPos),this._tweak.isLast=!0,this._block.fill(0,this._bPos),this.ubi(),this._bPos=0}write(t){this._hasData=!0;let s=t.length,e=0,i=this.blockSize-this._bPos;for(;s>0;){if(i>=s)return this._block.set(t.subarray(e)),void(this._bPos+=s);this._block.set(t.subarray(e,e+this.blockSize),this._bPos),this.hash(),e+=i,s-=i,i=this.blockSize}}clone(){const t=new k(this.blockSize,this.size,this._tfConstructor,c,c);return t._hasData=this._hasData,t._block.set(this._block),t._bPos=this._bPos,t._cfgBackup.set(this._cfgBackup),t._hVal.k64.set(this._hVal.k64),t._hVal.lock=!1,t._tweak.t64.set(this._tweak.t64),t._tweak.lock=!1,t}sum(){return this.clone().sumIn()}sumIn(){this._hasData&&this.finalize();const s=new Uint8Array(this.size);let e=this.size,i=0;this._tweak.makeOut();const o=this._hVal.k64.clone();for(;e>0;){const r=Math.min(e,this.blockSize),h=t.fromInt(i);this._block.fill(0),this._b64.at(0).set(h),this.ubi(),this._b64.at(0).xorEq(h),s.set(this._block.slice(0,r),i*this.blockSize),i++,e-=this.blockSize,this._hVal.k64.set(o)}return s}reset(){this._hasData=!1,this._bPos=0,this._hVal.k64.set(this._cfgBackup),this._hVal.lock=!1,this._tweak.makeMsg()}newEmpty(){return new k(this.blockSize,this.size,this._tfConstructor,c,c)}}class l extends k{constructor(t,s){super(32,t,h,s,c)}}class _ extends k{constructor(t,s){super(64,t,a,s,c)}}class u extends k{constructor(t,s){super(128,t,r,s,c)}}export class Skein256 extends l{constructor(t=32){e("sizeBytes",t).atLeast(1).atMost(32).throwNot(),super(t,c)}static New128(){return new Skein256(16)}static New160(){return new Skein256(20)}static New224(){return new Skein256(28)}static New256(){return new Skein256(32)}}export class Skein512 extends _{constructor(t=64){e("sizeBytes",t).atLeast(1).atMost(64).throwNot(),super(t,c)}static New128(){return new Skein512(16)}static New160(){return new Skein512(20)}static New224(){return new Skein512(28)}static New256(){return new Skein512(32)}static New384(){return new Skein512(48)}static New512(){return new Skein512(64)}}export class Skein1024 extends u{constructor(t=128){e("sizeBytes",t).atLeast(1).atMost(128).throwNot(),super(t,c)}static New384(){return new Skein1024(48)}static New512(){return new Skein1024(64)}static New1024(){return new Skein1024(128)}}export class SkeinMac256 extends l{constructor(t){i("key",t).atLeast(1).throwNot(),super(32,t)}}export class SkeinMac512 extends _{constructor(t){i("key",t).atLeast(1).throwNot(),super(64,t)}}export class SkeinMac1024 extends u{constructor(t){i("key",t).atLeast(1).throwNot(),super(128,t)}}export class SkeinXof256 extends l{constructor(t){e("outputSize",t).atLeast(1).atMost(536870912).throwNot(),super(t,c)}}export class SkeinXof512 extends _{constructor(t){e("outputSize",t).atLeast(1).atMost(536870912).throwNot(),super(t,c)}}export class SkeinXof1024 extends u{constructor(t){e("outputSize",t).atLeast(1).atMost(536870912).throwNot(),super(t,c)}}export class SkeinKdf256 extends k{constructor(t,s,e){super(32,t,h,s,e)}}export class SkeinKdf512 extends k{constructor(t,s,e){super(64,t,a,s,e)}}export class SkeinKdf1024 extends k{constructor(t,s,e){super(128,t,r,s,e)}}