gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.22 kB
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
export class Hmac{constructor(s,t){const i=function(s,t){if(t.length>s.blockSize)return s.reset(),s.write(t),s.sum();const i=new Uint8Array(s.blockSize);return i.set(t),i}(s,t),h=new Uint8Array(s.blockSize),e=new Uint8Array(s.blockSize);for(let t=0;t<s.blockSize;t++)h[t]=92^i[t],e[t]=54^i[t];this._oHash=s.newEmpty(),this._oHash.write(h),this._startIHash=s.newEmpty(),this._startIHash.write(e),this._iHash=this._startIHash.clone()}write(s){this._iHash.write(s)}sum(s=0){const t=this._iHash.sum(),i=this._oHash.clone();i.write(t);const h=i.sumIn();return s>0?h.slice(0,s):h}sumIn(s=0){const t=this._iHash.sumIn(),i=this._oHash.clone();i.write(t);const h=i.sumIn();return s>0?h.slice(0,s):h}reset(){this._iHash=this._startIHash.clone()}newEmpty(){const s=new Hmac(this._oHash,new Uint8Array(this._oHash.blockSize));return s._oHash=this._oHash,s._startIHash=this._startIHash,s._iHash=this._startIHash.clone(),s}clone(){const s=new Hmac(this._oHash,new Uint8Array(this._oHash.blockSize));return s._oHash=this._oHash,s._startIHash=this._startIHash,s._iHash=this._iHash.clone(),s}get size(){return this._iHash.size}get blockSize(){return this._iHash.blockSize}}