index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/hash-base/index.js';
  5. var __require = nodeEnv ? function (request) {
  6. return cc.require(request);
  7. } : function (request) {
  8. return __quick_compile_project__.require(request, __filename);
  9. };
  10. function __define (exports, require, module) {
  11. if (!nodeEnv) {__quick_compile_project__.registerModule(__filename, module);}'use strict'
  12. var Buffer = require('safe-buffer').Buffer
  13. var Transform = require('readable-stream').Transform
  14. var inherits = require('inherits')
  15. function throwIfNotStringOrBuffer (val, prefix) {
  16. if (!Buffer.isBuffer(val) && typeof val !== 'string') {
  17. throw new TypeError(prefix + ' must be a string or a buffer')
  18. }
  19. }
  20. function HashBase (blockSize) {
  21. Transform.call(this)
  22. this._block = Buffer.allocUnsafe(blockSize)
  23. this._blockSize = blockSize
  24. this._blockOffset = 0
  25. this._length = [0, 0, 0, 0]
  26. this._finalized = false
  27. }
  28. inherits(HashBase, Transform)
  29. HashBase.prototype._transform = function (chunk, encoding, callback) {
  30. var error = null
  31. try {
  32. this.update(chunk, encoding)
  33. } catch (err) {
  34. error = err
  35. }
  36. callback(error)
  37. }
  38. HashBase.prototype._flush = function (callback) {
  39. var error = null
  40. try {
  41. this.push(this.digest())
  42. } catch (err) {
  43. error = err
  44. }
  45. callback(error)
  46. }
  47. HashBase.prototype.update = function (data, encoding) {
  48. throwIfNotStringOrBuffer(data, 'Data')
  49. if (this._finalized) throw new Error('Digest already called')
  50. if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)
  51. // consume data
  52. var block = this._block
  53. var offset = 0
  54. while (this._blockOffset + data.length - offset >= this._blockSize) {
  55. for (var i = this._blockOffset; i < this._blockSize;) block[i++] = data[offset++]
  56. this._update()
  57. this._blockOffset = 0
  58. }
  59. while (offset < data.length) block[this._blockOffset++] = data[offset++]
  60. // update length
  61. for (var j = 0, carry = data.length * 8; carry > 0; ++j) {
  62. this._length[j] += carry
  63. carry = (this._length[j] / 0x0100000000) | 0
  64. if (carry > 0) this._length[j] -= 0x0100000000 * carry
  65. }
  66. return this
  67. }
  68. HashBase.prototype._update = function () {
  69. throw new Error('_update is not implemented')
  70. }
  71. HashBase.prototype.digest = function (encoding) {
  72. if (this._finalized) throw new Error('Digest already called')
  73. this._finalized = true
  74. var digest = this._digest()
  75. if (encoding !== undefined) digest = digest.toString(encoding)
  76. // reset state
  77. this._block.fill(0)
  78. this._blockOffset = 0
  79. for (var i = 0; i < 4; ++i) this._length[i] = 0
  80. return digest
  81. }
  82. HashBase.prototype._digest = function () {
  83. throw new Error('_digest is not implemented')
  84. }
  85. module.exports = HashBase
  86. }
  87. if (nodeEnv) {
  88. __define(__module.exports, __require, __module);
  89. }
  90. else {
  91. __quick_compile_project__.registerModuleFunc(__filename, function () {
  92. __define(__module.exports, __require, __module);
  93. });
  94. }
  95. })();