Sha256: 4cc85538f753e9035ceee03a91e172858650581fd8fdda4d385fb9fd85795f85

Contents?: true

Size: 1.84 KB

Versions: 35

Compression:

Stored size: 1.84 KB

Contents

var Buffer = require('safe-buffer').Buffer

// prototype class for hash functions
function Hash (blockSize, finalSize) {
  this._block = Buffer.alloc(blockSize)
  this._finalSize = finalSize
  this._blockSize = blockSize
  this._len = 0
}

Hash.prototype.update = function (data, enc) {
  if (typeof data === 'string') {
    enc = enc || 'utf8'
    data = Buffer.from(data, enc)
  }

  var block = this._block
  var blockSize = this._blockSize
  var length = data.length
  var accum = this._len

  for (var offset = 0; offset < length;) {
    var assigned = accum % blockSize
    var remainder = Math.min(length - offset, blockSize - assigned)

    for (var i = 0; i < remainder; i++) {
      block[assigned + i] = data[offset + i]
    }

    accum += remainder
    offset += remainder

    if ((accum % blockSize) === 0) {
      this._update(block)
    }
  }

  this._len += length
  return this
}

Hash.prototype.digest = function (enc) {
  var rem = this._len % this._blockSize

  this._block[rem] = 0x80

  // zero (rem + 1) trailing bits, where (rem + 1) is the smallest
  // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
  this._block.fill(0, rem + 1)

  if (rem >= this._finalSize) {
    this._update(this._block)
    this._block.fill(0)
  }

  var bits = this._len * 8

  // uint32
  if (bits <= 0xffffffff) {
    this._block.writeUInt32BE(bits, this._blockSize - 4)

  // uint64
  } else {
    var lowBits = (bits & 0xffffffff) >>> 0
    var highBits = (bits - lowBits) / 0x100000000

    this._block.writeUInt32BE(highBits, this._blockSize - 8)
    this._block.writeUInt32BE(lowBits, this._blockSize - 4)
  }

  this._update(this._block)
  var hash = this._hash()

  return enc ? hash.toString(enc) : hash
}

Hash.prototype._update = function () {
  throw new Error('_update must be implemented by subclass')
}

module.exports = Hash

Version data entries

35 entries across 34 versions & 12 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/sha.js/hash.js
disco_app-0.18.0 test/dummy/node_modules/sha.js/hash.js
disco_app-0.18.2 test/dummy/node_modules/sha.js/hash.js
disco_app-0.16.1 test/dummy/node_modules/sha.js/hash.js
disco_app-0.15.2 test/dummy/node_modules/sha.js/hash.js
disco_app-0.18.4 test/dummy/node_modules/sha.js/hash.js
disco_app-0.18.1 test/dummy/node_modules/sha.js/hash.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/sha.js/hash.js
disco_app-0.14.0 test/dummy/node_modules/sha.js/hash.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/sha.js/hash.js
tang-0.2.1 spec/tang_app/node_modules/sha.js/hash.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/sha.js/hash.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/sha.js/hash.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/sha.js/hash.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/sha.js/hash.js
tang-0.2.0 spec/tang_app/node_modules/sha.js/hash.js
tang-0.1.0 spec/tang_app/node_modules/sha.js/hash.js
tang-0.0.9 spec/tang_app/node_modules/sha.js/hash.js
enju_library-0.3.8 spec/dummy/node_modules/sha.js/hash.js
ilog-0.4.1 node_modules/sha.js/hash.js