UNPKG

@swtc/common

Version:
75 lines 2.51 kB
import { toArray, join32 } from "./utils"; import { funcAssert as assert } from "../functions"; export class BlockHash { constructor(blockSize = 512, outSize = 256, hmacStrength = 192, padLength = 64) { this.pending = null; this.pendingTotal = 0; this.endian = "big"; this.blockSize = blockSize; this.outSize = outSize; this.hmacStrength = hmacStrength; this.padLength = padLength / 8; this._delta8 = this.blockSize / 8; this._delta32 = this.blockSize / 32; } update(msg, enc = "") { msg = toArray(msg, enc); if (!this.pending) this.pending = msg; else this.pending = this.pending.concat(msg); this.pendingTotal += msg.length; if (this.pending.length >= this._delta8) { msg = this.pending; const r = msg.length % this._delta8; this.pending = msg.slice(msg.length - r, msg.length); if (this.pending.length === 0) this.pending = null; msg = join32(msg, 0, msg.length - r, this.endian); for (let i = 0; i < msg.length; i += this._delta32) this._update(msg, i); } return this; } digest(enc = "") { this.update(this.pad()); assert(this.pending === null); return this._digest(enc); } pad() { var len = this.pendingTotal; var bytes = this._delta8; var k = bytes - ((len + this.padLength) % bytes); var res = new Array(k + this.padLength); res[0] = 0x80; for (var i = 1; i < k; i++) res[i] = 0; len <<= 3; if (this.endian === "big") { for (var t = 8; t < this.padLength; t++) res[i++] = 0; res[i++] = 0; res[i++] = 0; res[i++] = 0; res[i++] = 0; res[i++] = (len >>> 24) & 0xff; res[i++] = (len >>> 16) & 0xff; res[i++] = (len >>> 8) & 0xff; res[i++] = len & 0xff; } else { res[i++] = len & 0xff; res[i++] = (len >>> 8) & 0xff; res[i++] = (len >>> 16) & 0xff; res[i++] = (len >>> 24) & 0xff; res[i++] = 0; res[i++] = 0; res[i++] = 0; res[i++] = 0; for (t = 8; t < this.padLength; t++) res[i++] = 0; } return res; } } //# sourceMappingURL=common.js.map