generateRandomUuid.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.uuid.generateRandomUuid"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.uuid.generateRandomUuid"] = true;
  8. dojo.provide("dojox.uuid.generateRandomUuid");
  9. dojox.uuid.generateRandomUuid = function(){
  10. // summary:
  11. // This function generates random UUIDs, meaning "version 4" UUIDs.
  12. // description:
  13. // A typical generated value would be something like this:
  14. // "3b12f1df-5232-4804-897e-917bf397618a"
  15. //
  16. // For more information about random UUIDs, see sections 4.4 and
  17. // 4.5 of RFC 4122: http://tools.ietf.org/html/rfc4122#section-4.4
  18. //
  19. // This generator function is designed to be small and fast,
  20. // but not necessarily good.
  21. //
  22. // Small: This generator has a small footprint. Once comments are
  23. // stripped, it's only about 25 lines of code, and it doesn't
  24. // dojo.require() any other modules.
  25. //
  26. // Fast: This generator can generate lots of new UUIDs fairly quickly
  27. // (at least, more quickly than the other dojo UUID generators).
  28. //
  29. // Not necessarily good: We use Math.random() as our source
  30. // of randomness, which may or may not provide much randomness.
  31. // examples:
  32. // var string = dojox.uuid.generateRandomUuid();
  33. var HEX_RADIX = 16;
  34. function _generateRandomEightCharacterHexString(){
  35. // Make random32bitNumber be a randomly generated floating point number
  36. // between 0 and (4,294,967,296 - 1), inclusive.
  37. var random32bitNumber = Math.floor( (Math.random() % 1) * Math.pow(2, 32) );
  38. var eightCharacterHexString = random32bitNumber.toString(HEX_RADIX);
  39. while(eightCharacterHexString.length < 8){
  40. eightCharacterHexString = "0" + eightCharacterHexString;
  41. }
  42. return eightCharacterHexString; // for example: "3B12F1DF"
  43. }
  44. var hyphen = "-";
  45. var versionCodeForRandomlyGeneratedUuids = "4"; // 8 == binary2hex("0100")
  46. var variantCodeForDCEUuids = "8"; // 8 == binary2hex("1000")
  47. var a = _generateRandomEightCharacterHexString();
  48. var b = _generateRandomEightCharacterHexString();
  49. b = b.substring(0, 4) + hyphen + versionCodeForRandomlyGeneratedUuids + b.substring(5, 8);
  50. var c = _generateRandomEightCharacterHexString();
  51. c = variantCodeForDCEUuids + c.substring(1, 4) + hyphen + c.substring(4, 8);
  52. var d = _generateRandomEightCharacterHexString();
  53. var returnValue = a + hyphen + b + hyphen + c + d;
  54. returnValue = returnValue.toLowerCase();
  55. return returnValue; // String
  56. };
  57. }