hash.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/sha.js/hash.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);}var Buffer = require('safe-buffer').Buffer
  12. // prototype class for hash functions
  13. function Hash (blockSize, finalSize) {
  14. this._block = Buffer.alloc(blockSize)
  15. this._finalSize = finalSize
  16. this._blockSize = blockSize
  17. this._len = 0
  18. }
  19. Hash.prototype.update = function (data, enc) {
  20. if (typeof data === 'string') {
  21. enc = enc || 'utf8'
  22. data = Buffer.from(data, enc)
  23. }
  24. var block = this._block
  25. var blockSize = this._blockSize
  26. var length = data.length
  27. var accum = this._len
  28. for (var offset = 0; offset < length;) {
  29. var assigned = accum % blockSize
  30. var remainder = Math.min(length - offset, blockSize - assigned)
  31. for (var i = 0; i < remainder; i++) {
  32. block[assigned + i] = data[offset + i]
  33. }
  34. accum += remainder
  35. offset += remainder
  36. if ((accum % blockSize) === 0) {
  37. this._update(block)
  38. }
  39. }
  40. this._len += length
  41. return this
  42. }
  43. Hash.prototype.digest = function (enc) {
  44. var rem = this._len % this._blockSize
  45. this._block[rem] = 0x80
  46. // zero (rem + 1) trailing bits, where (rem + 1) is the smallest
  47. // non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
  48. this._block.fill(0, rem + 1)
  49. if (rem >= this._finalSize) {
  50. this._update(this._block)
  51. this._block.fill(0)
  52. }
  53. var bits = this._len * 8
  54. // uint32
  55. if (bits <= 0xffffffff) {
  56. this._block.writeUInt32BE(bits, this._blockSize - 4)
  57. // uint64
  58. } else {
  59. var lowBits = (bits & 0xffffffff) >>> 0
  60. var highBits = (bits - lowBits) / 0x100000000
  61. this._block.writeUInt32BE(highBits, this._blockSize - 8)
  62. this._block.writeUInt32BE(lowBits, this._blockSize - 4)
  63. }
  64. this._update(this._block)
  65. var hash = this._hash()
  66. return enc ? hash.toString(enc) : hash
  67. }
  68. Hash.prototype._update = function () {
  69. throw new Error('_update must be implemented by subclass')
  70. }
  71. module.exports = Hash
  72. }
  73. if (nodeEnv) {
  74. __define(__module.exports, __require, __module);
  75. }
  76. else {
  77. __quick_compile_project__.registerModuleFunc(__filename, function () {
  78. __define(__module.exports, __require, __module);
  79. });
  80. }
  81. })();