How unique is UUID?

后端 未结 11 1871
终归单人心
终归单人心 2020-11-22 05:40

How safe is it to use UUID to uniquely identify something (I\'m using it for files uploaded to the server)? As I understand it, it is based off random numbers. However, it s

11条回答
  •  被撕碎了的回忆
    2020-11-22 06:02

    Here's a testing snippet for you to test it's uniquenes. inspired by @scalabl3's comment

    Funny thing is, you could generate 2 in a row that were identical, of course at mind-boggling levels of coincidence, luck and divine intervention, yet despite the unfathomable odds, it's still possible! :D Yes, it won't happen. just saying for the amusement of thinking about that moment when you created a duplicate! Screenshot video! – scalabl3 Oct 20 '15 at 19:11

    If you feel lucky, check the checkbox, it only checks the currently generated id's. If you wish a history check, leave it unchecked. Please note, you might run out of ram at some point if you leave it unchecked. I tried to make it cpu friendly so you can abort quickly when needed, just hit the run snippet button again or leave the page.

    Math.log2 = Math.log2 || function(n){ return Math.log(n) / Math.log(2); }
      Math.trueRandom = (function() {
      var crypt = window.crypto || window.msCrypto;
    
      if (crypt && crypt.getRandomValues) {
          // if we have a crypto library, use it
          var random = function(min, max) {
              var rval = 0;
              var range = max - min;
              if (range < 2) {
                  return min;
              }
    
              var bits_needed = Math.ceil(Math.log2(range));
              if (bits_needed > 53) {
                throw new Exception("We cannot generate numbers larger than 53 bits.");
              }
              var bytes_needed = Math.ceil(bits_needed / 8);
              var mask = Math.pow(2, bits_needed) - 1;
              // 7776 -> (2^13 = 8192) -1 == 8191 or 0x00001111 11111111
    
              // Create byte array and fill with N random numbers
              var byteArray = new Uint8Array(bytes_needed);
              crypt.getRandomValues(byteArray);
    
              var p = (bytes_needed - 1) * 8;
              for(var i = 0; i < bytes_needed; i++ ) {
                  rval += byteArray[i] * Math.pow(2, p);
                  p -= 8;
              }
    
              // Use & to apply the mask and reduce the number of recursive lookups
              rval = rval & mask;
    
              if (rval >= range) {
                  // Integer out of acceptable range
                  return random(min, max);
              }
              // Return an integer that falls within the range
              return min + rval;
          }
          return function() {
              var r = random(0, 1000000000) / 1000000000;
              return r;
          };
      } else {
          // From http://baagoe.com/en/RandomMusings/javascript/
          // Johannes Baagøe , 2010
          function Mash() {
              var n = 0xefc8249d;
    
              var mash = function(data) {
                  data = data.toString();
                  for (var i = 0; i < data.length; i++) {
                      n += data.charCodeAt(i);
                      var h = 0.02519603282416938 * n;
                      n = h >>> 0;
                      h -= n;
                      h *= n;
                      n = h >>> 0;
                      h -= n;
                      n += h * 0x100000000; // 2^32
                  }
                  return (n >>> 0) * 2.3283064365386963e-10; // 2^-32
              };
    
              mash.version = 'Mash 0.9';
              return mash;
          }
    
          // From http://baagoe.com/en/RandomMusings/javascript/
          function Alea() {
              return (function(args) {
                  // Johannes Baagøe , 2010
                  var s0 = 0;
                  var s1 = 0;
                  var s2 = 0;
                  var c = 1;
    
                  if (args.length == 0) {
                      args = [+new Date()];
                  }
                  var mash = Mash();
                  s0 = mash(' ');
                  s1 = mash(' ');
                  s2 = mash(' ');
    
                  for (var i = 0; i < args.length; i++) {
                      s0 -= mash(args[i]);
                      if (s0 < 0) {
                          s0 += 1;
                      }
                      s1 -= mash(args[i]);
                      if (s1 < 0) {
                          s1 += 1;
                      }
                      s2 -= mash(args[i]);
                      if (s2 < 0) {
                          s2 += 1;
                      }
                  }
                  mash = null;
    
                  var random = function() {
                      var t = 2091639 * s0 + c * 2.3283064365386963e-10; // 2^-32
                      s0 = s1;
                      s1 = s2;
                      return s2 = t - (c = t | 0);
                  };
                  random.uint32 = function() {
                      return random() * 0x100000000; // 2^32
                  };
                  random.fract53 = function() {
                      return random() +
                          (random() * 0x200000 | 0) * 1.1102230246251565e-16; // 2^-53
                  };
                  random.version = 'Alea 0.9';
                  random.args = args;
                  return random;
    
              }(Array.prototype.slice.call(arguments)));
          };
          return Alea();
      }
    }());
    
    Math.guid = function() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)    {
          var r = Math.trueRandom() * 16 | 0,
              v = c == 'x' ? r : (r & 0x3 | 0x8);
          return v.toString(16);
      });
    };
    function logit(item1, item2) {
        console.log("Do "+item1+" and "+item2+" equal? "+(item1 == item2 ? "OMG! take a screenshot and you'll be epic on the world of cryptography, buy a lottery ticket now!":"No they do not. shame. no fame")+ ", runs: "+window.numberofRuns);
    }
    numberofRuns = 0;
    function test() {
       window.numberofRuns++;
       var x = Math.guid();
       var y = Math.guid();
       var test = x == y || historyTest(x,y);
    
       logit(x,y);
       return test;
    
    }
    historyArr = [];
    historyCount = 0;
    function historyTest(item1, item2) {
        if(window.luckyDog) {
           return false;
        }
        for(var i = historyCount; i > -1; i--) {
            logit(item1,window.historyArr[i]);
            if(item1 == history[i]) {
                
                return true;
            }
            logit(item2,window.historyArr[i]);
            if(item2 == history[i]) {
                
                return true;
            }
    
        }
        window.historyArr.push(item1);
        window.historyArr.push(item2);
        window.historyCount+=2;
        return false;
    }
    luckyDog = false;
    document.body.onload = function() {
    document.getElementById('runit').onclick  = function() {
    window.luckyDog = document.getElementById('lucky').checked;
    var val = document.getElementById('input').value
    if(val.trim() == '0') {
        var intervaltimer = window.setInterval(function() {
             var test = window.test();
             if(test) {
                window.clearInterval(intervaltimer);
             }
        },0);
    }
    else {
       var num = parseInt(val);
       if(num > 0) {
            var intervaltimer = window.setInterval(function() {
             var test = window.test();
             num--;
             if(num < 0 || test) {
        
             window.clearInterval(intervaltimer);
             }
        },0);
       }
    }
    };
    };
    Please input how often the calulation should run. set to 0 for forever. Check the checkbox if you feel lucky.

提交回复
热议问题