react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / create-hash / node_modules / sha.js / hash.js
80556 views//prototype class for hash functions1function Hash (blockSize, finalSize) {2this._block = new Buffer(blockSize) //new Uint32Array(blockSize/4)3this._finalSize = finalSize4this._blockSize = blockSize5this._len = 06this._s = 07}89Hash.prototype.update = function (data, enc) {10if ("string" === typeof data) {11enc = enc || "utf8"12data = new Buffer(data, enc)13}1415var l = this._len += data.length16var s = this._s || 017var f = 018var buffer = this._block1920while (s < l) {21var t = Math.min(data.length, f + this._blockSize - (s % this._blockSize))22var ch = (t - f)2324for (var i = 0; i < ch; i++) {25buffer[(s % this._blockSize) + i] = data[i + f]26}2728s += ch29f += ch3031if ((s % this._blockSize) === 0) {32this._update(buffer)33}34}35this._s = s3637return this38}3940Hash.prototype.digest = function (enc) {41// Suppose the length of the message M, in bits, is l42var l = this._len * 84344// Append the bit 1 to the end of the message45this._block[this._len % this._blockSize] = 0x804647// and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize48this._block.fill(0, this._len % this._blockSize + 1)4950if (l % (this._blockSize * 8) >= this._finalSize * 8) {51this._update(this._block)52this._block.fill(0)53}5455// to this append the block which is equal to the number l written in binary56// TODO: handle case where l is > Math.pow(2, 29)57this._block.writeInt32BE(l, this._blockSize - 4)5859var hash = this._update(this._block) || this._hash()6061return enc ? hash.toString(enc) : hash62}6364Hash.prototype._update = function () {65throw new Error('_update must be implemented by subclass')66}6768module.exports = Hash697071