{"version":3,"sources":["random/random.js","random/deprecated.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,gF;AACA,2E;AACA,4E;AACA,6E;AACA,2E;AACA,2E;AACA,gF;AACA,gC;;AAEA,oB;AACA,yC;;AAEA,qE;AACA,iD;AACA,wB;AACA,mB;AACA,uB;;AAEA,+B;AACA,6B;AACA,6C;AACA,gC;AACA,wC;AACA,oB;AACA,e;AACA,e;AACA,oB;AACA,e;AACA,qC;AACA,O;AACA,yD;AACA,M;;AAEA,8B;AACA,gB;AACA,G;;AAEA,2B;AACA,e;AACA,e;AACA,e;AACA,c;;AAEA,2B;AACA,yB;AACA,K;AACA,sB;AACA,mB;AACA,mB;AACA,mB;;AAEA,2C;AACA,0B;AACA,mB;AACA,gB;AACA,O;AACA,0B;AACA,mB;AACA,gB;AACA,O;AACA,0B;AACA,mB;AACA,gB;AACA,O;AACA,K;AACA,gB;;AAEA,6B;AACA,iE;AACA,c;AACA,c;AACA,kC;AACA,M;AACA,gC;AACA,4C;AACA,M;AACA,iC;AACA,uB;AACA,oE;AACA,M;AACA,gC;AACA,uB;AACA,kB;;AAEA,6C;AACA,E;;AAEA,mF;AACA,2E;AACA,iB;;AAEA,8E;AACA,0E;AACA,6E;AACA,yC;AACA,4C;AACA,kB;AACA,8B;AACA,4C;AACA,E;;AAEA,kD;AACA,kB;AACA,kB;AACA,uB;AACA,0B;AACA,oD;AACA,uD;AACA,8D;AACA,6C;AACA,mC;AACA,yC;AACA,sD;AACA,U;AACA,qD;AACA,G;AACA,E;;AAEA,yD;AACA,kB;AACA,kC;AACA,yC;AACA,c;AACA,mE;AACA,qD;AACA,S;AACA,+C;AACA,iB;AACA,kE;AACA,qD;AACA,K;AACA,uC;AACA,2E;AACA,wD;AACA,uC;AACA,U;AACA,uB;AACA,sC;AACA,sD;AACA,K;AACA,8B;AACA,G;AACA,E;;AAEA,+D;AACA,+D;AACA,kB;AACA,kB;AACA,wC;AACA,sC;AACA,G;AACA,yB;AACA,E;;AAEA,sD;AACA,kB;AACA,uE;AACA,4B;AACA,+B;AACA,oB;;AAEA,4D;AACA,E;;AAEA,0D;AACA,kB;AACA,mE;AACA,e;AACA,+B;AACA,oB;AACA,sD;AACA,E;;AAEA,6D;AACA,iE;AACA,wC;AACA,0C;AACA,M;AACA,gC;AACA,E;;AAEA,8E;AACA,sC;;AAEA,iB;AACA,qE;AACA,sC;AACA,kC;AACA,mD;AACA,sC;AACA,uB;AACA,wC;AACA,Q;;AAEA,mE;AACA,sC;AACA,kC;AACA,kD;AACA,sC;AACA,uB;AACA,uC;AACA,Q;;AAEA,4E;;AAEA,iB;AACA,qC;AACA,qD;AACA,iC;AACA,I;AACA,kF;;AAEA,sC;AACA,+B;AACA,8C;AACA,G;AACA,wC;AACA,E;;;;;;;;;;;;;;;;;;;ACzNA,iE;AACA,gE;AACA,oB;AACA,wB;AACA,2B;AACA,sC;AACA,a;AACA,gC;AACA,qC;AACA,G;AACA,c;AACA,iE;AACA,qC;;AAEA,wB;AACA,c;AACA,E","file":"/packages/random.js","sourcesContent":["// We use cryptographically strong PRNGs (crypto.getRandomBytes() on the server,\n// window.crypto.getRandomValues() in the browser) when available. If these\n// PRNGs fail, we fall back to the Alea PRNG, which is not cryptographically\n// strong, and we seed it with various sources such as the date, Math.random,\n// and window size on the client.  When using crypto.getRandomValues(), our\n// primitive is hexString(), from which we construct fraction(). When using\n// window.crypto.getRandomValues() or alea, the primitive is fraction and we use\n// that to construct hex string.\n\nif (Meteor.isServer)\n  var nodeCrypto = Npm.require('crypto');\n\n// see http://baagoe.org/en/wiki/Better_random_numbers_for_javascript\n// for a full discussion and Alea implementation.\nvar Alea = function () {\n  function Mash() {\n    var n = 0xefc8249d;\n\n    var mash = function(data) {\n      data = data.toString();\n      for (var i = 0; i < data.length; i++) {\n        n += data.charCodeAt(i);\n        var h = 0.02519603282416938 * n;\n        n = h >>> 0;\n        h -= n;\n        h *= n;\n        n = h >>> 0;\n        h -= n;\n        n += h * 0x100000000; // 2^32\n      }\n      return (n >>> 0) * 2.3283064365386963e-10; // 2^-32\n    };\n\n    mash.version = 'Mash 0.9';\n    return mash;\n  }\n\n  return (function (args) {\n    var s0 = 0;\n    var s1 = 0;\n    var s2 = 0;\n    var c = 1;\n\n    if (args.length == 0) {\n      args = [+new Date];\n    }\n    var mash = Mash();\n    s0 = mash(' ');\n    s1 = mash(' ');\n    s2 = mash(' ');\n\n    for (var i = 0; i < args.length; i++) {\n      s0 -= mash(args[i]);\n      if (s0 < 0) {\n        s0 += 1;\n      }\n      s1 -= mash(args[i]);\n      if (s1 < 0) {\n        s1 += 1;\n      }\n      s2 -= mash(args[i]);\n      if (s2 < 0) {\n        s2 += 1;\n      }\n    }\n    mash = null;\n\n    var random = function() {\n      var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32\n      s0 = s1;\n      s1 = s2;\n      return s2 = t - (c = t | 0);\n    };\n    random.uint32 = function() {\n      return random() * 0x100000000; // 2^32\n    };\n    random.fract53 = function() {\n      return random() +\n        (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53\n    };\n    random.version = 'Alea 0.9';\n    random.args = args;\n    return random;\n\n  } (Array.prototype.slice.call(arguments)));\n};\n\nvar UNMISTAKABLE_CHARS = \"23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz\";\nvar BASE64_CHARS = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\" +\n  \"0123456789-_\";\n\n// If seeds are provided, then the alea PRNG will be used, since cryptographic\n// PRNGs (Node crypto and window.crypto.getRandomValues) don't allow us to\n// specify seeds. The caller is responsible for making sure to provide a seed\n// for alea if a csprng is not available.\nvar RandomGenerator = function (seedArray) {\n  var self = this;\n  if (seedArray !== undefined)\n    self.alea = Alea.apply(null, seedArray);\n};\n\nRandomGenerator.prototype.fraction = function () {\n  var self = this;\n  if (self.alea) {\n    return self.alea();\n  } else if (nodeCrypto) {\n    var numerator = parseInt(self.hexString(8), 16);\n    return numerator * 2.3283064365386963e-10; // 2^-32\n  } else if (typeof window !== \"undefined\" && window.crypto &&\n             window.crypto.getRandomValues) {\n    var array = new Uint32Array(1);\n    window.crypto.getRandomValues(array);\n    return array[0] * 2.3283064365386963e-10; // 2^-32\n  } else {\n    throw new Error('No random generator available');\n  }\n};\n\nRandomGenerator.prototype.hexString = function (digits) {\n  var self = this;\n  if (nodeCrypto && ! self.alea) {\n    var numBytes = Math.ceil(digits / 2);\n    var bytes;\n    // Try to get cryptographically strong randomness. Fall back to\n    // non-cryptographically strong if not available.\n    try {\n      bytes = nodeCrypto.randomBytes(numBytes);\n    } catch (e) {\n      // XXX should re-throw any error except insufficient entropy\n      bytes = nodeCrypto.pseudoRandomBytes(numBytes);\n    }\n    var result = bytes.toString(\"hex\");\n    // If the number of digits is odd, we'll have generated an extra 4 bits\n    // of randomness, so we need to trim the last digit.\n    return result.substring(0, digits);\n  } else {\n    var hexDigits = [];\n    for (var i = 0; i < digits; ++i) {\n      hexDigits.push(self.choice(\"0123456789abcdef\"));\n    }\n    return hexDigits.join('');\n  }\n};\n\nRandomGenerator.prototype._randomString = function (charsCount,\n                                                    alphabet) {\n  var self = this;\n  var digits = [];\n  for (var i = 0; i < charsCount; i++) {\n    digits[i] = self.choice(alphabet);\n  }\n  return digits.join(\"\");\n};\n\nRandomGenerator.prototype.id = function (charsCount) {\n  var self = this;\n  // 17 characters is around 96 bits of entropy, which is the amount of\n  // state in the Alea PRNG.\n  if (charsCount === undefined)\n    charsCount = 17;\n\n  return self._randomString(charsCount, UNMISTAKABLE_CHARS);\n};\n\nRandomGenerator.prototype.secret = function (charsCount) {\n  var self = this;\n  // Default to 256 bits of entropy, or 43 characters at 6 bits per\n  // character.\n  if (charsCount === undefined)\n    charsCount = 43;\n  return self._randomString(charsCount, BASE64_CHARS);\n};\n\nRandomGenerator.prototype.choice = function (arrayOrString) {\n  var index = Math.floor(this.fraction() * arrayOrString.length);\n  if (typeof arrayOrString === \"string\")\n    return arrayOrString.substr(index, 1);\n  else\n    return arrayOrString[index];\n};\n\n// instantiate RNG.  Heuristically collect entropy from various sources when a\n// cryptographic PRNG isn't available.\n\n// client sources\nvar height = (typeof window !== 'undefined' && window.innerHeight) ||\n      (typeof document !== 'undefined'\n       && document.documentElement\n       && document.documentElement.clientHeight) ||\n      (typeof document !== 'undefined'\n       && document.body\n       && document.body.clientHeight) ||\n      1;\n\nvar width = (typeof window !== 'undefined' && window.innerWidth) ||\n      (typeof document !== 'undefined'\n       && document.documentElement\n       && document.documentElement.clientWidth) ||\n      (typeof document !== 'undefined'\n       && document.body\n       && document.body.clientWidth) ||\n      1;\n\nvar agent = (typeof navigator !== 'undefined' && navigator.userAgent) || \"\";\n\nif (nodeCrypto ||\n    (typeof window !== \"undefined\" &&\n     window.crypto && window.crypto.getRandomValues))\n  Random = new RandomGenerator();\nelse\n  Random = new RandomGenerator([new Date(), height, width, agent, Math.random()]);\n\nRandom.createWithSeeds = function () {\n  if (arguments.length === 0) {\n    throw new Error('No seeds were provided');\n  }\n  return new RandomGenerator(arguments);\n};\n","// Before this package existed, we used to use this Meteor.uuid()\n// implementing the RFC 4122 v4 UUID. It is no longer documented\n// and will go away.\n// XXX COMPAT WITH 0.5.6\nMeteor.uuid = function () {\n  var HEX_DIGITS = \"0123456789abcdef\";\n  var s = [];\n  for (var i = 0; i < 36; i++) {\n    s[i] = Random.choice(HEX_DIGITS);\n  }\n  s[14] = \"4\";\n  s[19] = HEX_DIGITS.substr((parseInt(s[19],16) & 0x3) | 0x8, 1);\n  s[8] = s[13] = s[18] = s[23] = \"-\";\n\n  var uuid = s.join(\"\");\n  return uuid;\n};\n"]}