index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict'
  2. var Buffer = require('safe-buffer').Buffer
  3. var Transform = require('stream').Transform
  4. var inherits = require('inherits')
  5. function HashBase (blockSize) {
  6. Transform.call(this)
  7. this._block = Buffer.allocUnsafe(blockSize)
  8. this._blockSize = blockSize
  9. this._blockOffset = 0
  10. this._length = [0, 0, 0, 0]
  11. this._finalized = false
  12. }
  13. inherits(HashBase, Transform)
  14. HashBase.prototype._transform = function (chunk, encoding, callback) {
  15. var error = null
  16. try {
  17. this.update(chunk, encoding)
  18. } catch (err) {
  19. error = err
  20. }
  21. callback(error)
  22. }
  23. HashBase.prototype._flush = function (callback) {
  24. var error = null
  25. try {
  26. this.push(this.digest())
  27. } catch (err) {
  28. error = err
  29. }
  30. callback(error)
  31. }
  32. var useUint8Array = typeof Uint8Array !== 'undefined'
  33. var useArrayBuffer = typeof ArrayBuffer !== 'undefined' &&
  34. typeof Uint8Array !== 'undefined' &&
  35. ArrayBuffer.isView &&
  36. (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT)
  37. function toBuffer (data, encoding) {
  38. // No need to do anything for exact instance
  39. // This is only valid when safe-buffer.Buffer === buffer.Buffer, i.e. when Buffer.from/Buffer.alloc existed
  40. if (data instanceof Buffer) return data
  41. // Convert strings to Buffer
  42. if (typeof data === 'string') return Buffer.from(data, encoding)
  43. /*
  44. * Wrap any TypedArray instances and DataViews
  45. * Makes sense only on engines with full TypedArray support -- let Buffer detect that
  46. */
  47. if (useArrayBuffer && ArrayBuffer.isView(data)) {
  48. if (data.byteLength === 0) return Buffer.alloc(0) // Bug in Node.js <6.3.1, which treats this as out-of-bounds
  49. var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
  50. // Recheck result size, as offset/length doesn't work on Node.js <5.10
  51. // We just go to Uint8Array case if this fails
  52. if (res.byteLength === data.byteLength) return res
  53. }
  54. /*
  55. * Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
  56. * Doesn't make sense with other TypedArray instances
  57. */
  58. if (useUint8Array && data instanceof Uint8Array) return Buffer.from(data)
  59. /*
  60. * Old Buffer polyfill on an engine that doesn't have TypedArray support
  61. * Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
  62. * Convert to our current Buffer implementation
  63. */
  64. if (
  65. Buffer.isBuffer(data) &&
  66. data.constructor &&
  67. typeof data.constructor.isBuffer === 'function' &&
  68. data.constructor.isBuffer(data)
  69. ) {
  70. return Buffer.from(data)
  71. }
  72. throw new TypeError('The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.')
  73. }
  74. HashBase.prototype.update = function (data, encoding) {
  75. if (this._finalized) throw new Error('Digest already called')
  76. data = toBuffer(data, encoding) // asserts correct input type
  77. // consume data
  78. var block = this._block
  79. var offset = 0
  80. while (this._blockOffset + data.length - offset >= this._blockSize) {
  81. for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++]
  82. this._update()
  83. this._blockOffset = 0
  84. }
  85. while (offset < data.length) block[this._blockOffset++] = data[offset++]
  86. // update length
  87. for (var j = 0, carry = data.length * 8; carry > 0; ++j) {
  88. this._length[j] += carry
  89. carry = (this._length[j] / 0x0100000000) | 0
  90. if (carry > 0) this._length[j] -= 0x0100000000 * carry
  91. }
  92. return this
  93. }
  94. HashBase.prototype._update = function () {
  95. throw new Error('_update is not implemented')
  96. }
  97. HashBase.prototype.digest = function (encoding) {
  98. if (this._finalized) throw new Error('Digest already called')
  99. this._finalized = true
  100. var digest = this._digest()
  101. if (encoding !== undefined) digest = digest.toString(encoding)
  102. // reset state
  103. this._block.fill(0)
  104. this._blockOffset = 0
  105. for (var i = 0; i < 4; ++i) this._length[i] = 0
  106. return digest
  107. }
  108. HashBase.prototype._digest = function () {
  109. throw new Error('_digest is not implemented')
  110. }
  111. module.exports = HashBase