index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (function() {
  2. var nodeEnv = typeof require !== 'undefined' && typeof process !== 'undefined';
  3. var __module = nodeEnv ? module : {exports:{}};
  4. var __filename = 'preview-scripts/__node_modules/brorand/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);}var r;
  12. module.exports = function rand(len) {
  13. if (!r)
  14. r = new Rand(null);
  15. return r.generate(len);
  16. };
  17. function Rand(rand) {
  18. this.rand = rand;
  19. }
  20. module.exports.Rand = Rand;
  21. Rand.prototype.generate = function generate(len) {
  22. return this._rand(len);
  23. };
  24. // Emulate crypto API using randy
  25. Rand.prototype._rand = function _rand(n) {
  26. if (this.rand.getBytes)
  27. return this.rand.getBytes(n);
  28. var res = new Uint8Array(n);
  29. for (var i = 0; i < res.length; i++)
  30. res[i] = this.rand.getByte();
  31. return res;
  32. };
  33. if (typeof self === 'object') {
  34. if (self.crypto && self.crypto.getRandomValues) {
  35. // Modern browsers
  36. Rand.prototype._rand = function _rand(n) {
  37. var arr = new Uint8Array(n);
  38. self.crypto.getRandomValues(arr);
  39. return arr;
  40. };
  41. } else if (self.msCrypto && self.msCrypto.getRandomValues) {
  42. // IE
  43. Rand.prototype._rand = function _rand(n) {
  44. var arr = new Uint8Array(n);
  45. self.msCrypto.getRandomValues(arr);
  46. return arr;
  47. };
  48. // Safari's WebWorkers do not have `crypto`
  49. } else if (typeof window === 'object') {
  50. // Old junk
  51. Rand.prototype._rand = function() {
  52. throw new Error('Not implemented yet');
  53. };
  54. }
  55. } else {
  56. // Node.js or Web worker with no crypto support
  57. try {
  58. var crypto = require('crypto');
  59. if (typeof crypto.randomBytes !== 'function')
  60. throw new Error('Not supported');
  61. Rand.prototype._rand = function _rand(n) {
  62. return crypto.randomBytes(n);
  63. };
  64. } catch (e) {
  65. }
  66. }
  67. }
  68. if (nodeEnv) {
  69. __define(__module.exports, __require, __module);
  70. }
  71. else {
  72. __quick_compile_project__.registerModuleFunc(__filename, function () {
  73. __define(__module.exports, __require, __module);
  74. });
  75. }
  76. })();