react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-sign / node_modules / elliptic / node_modules / hash.js / lib / hash / common.js
80644 viewsvar hash = require('../hash');1var utils = hash.utils;2var assert = utils.assert;34function BlockHash() {5this.pending = null;6this.pendingTotal = 0;7this.blockSize = this.constructor.blockSize;8this.outSize = this.constructor.outSize;9this.hmacStrength = this.constructor.hmacStrength;10this.padLength = this.constructor.padLength / 8;11this.endian = 'big';1213this._delta8 = this.blockSize / 8;14this._delta32 = this.blockSize / 32;15}16exports.BlockHash = BlockHash;1718BlockHash.prototype.update = function update(msg, enc) {19// Convert message to array, pad it, and join into 32bit blocks20msg = utils.toArray(msg, enc);21if (!this.pending)22this.pending = msg;23else24this.pending = this.pending.concat(msg);25this.pendingTotal += msg.length;2627// Enough data, try updating28if (this.pending.length >= this._delta8) {29msg = this.pending;3031// Process pending data in blocks32var r = msg.length % this._delta8;33this.pending = msg.slice(msg.length - r, msg.length);34if (this.pending.length === 0)35this.pending = null;3637msg = utils.join32(msg, 0, msg.length - r, this.endian);38for (var i = 0; i < msg.length; i += this._delta32)39this._update(msg, i, i + this._delta32);40}4142return this;43};4445BlockHash.prototype.digest = function digest(enc) {46this.update(this._pad());47assert(this.pending === null);4849return this._digest(enc);50};5152BlockHash.prototype._pad = function pad() {53var len = this.pendingTotal;54var bytes = this._delta8;55var k = bytes - ((len + this.padLength) % bytes);56var res = new Array(k + this.padLength);57res[0] = 0x80;58for (var i = 1; i < k; i++)59res[i] = 0;6061// Append length62len <<= 3;63if (this.endian === 'big') {64for (var t = 8; t < this.padLength; t++)65res[i++] = 0;6667res[i++] = 0;68res[i++] = 0;69res[i++] = 0;70res[i++] = 0;71res[i++] = (len >>> 24) & 0xff;72res[i++] = (len >>> 16) & 0xff;73res[i++] = (len >>> 8) & 0xff;74res[i++] = len & 0xff;75} else {76res[i++] = len & 0xff;77res[i++] = (len >>> 8) & 0xff;78res[i++] = (len >>> 16) & 0xff;79res[i++] = (len >>> 24) & 0xff;80res[i++] = 0;81res[i++] = 0;82res[i++] = 0;83res[i++] = 0;8485for (var t = 8; t < this.padLength; t++)86res[i++] = 0;87}8889return res;90};919293