UUID.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. o2.widget = o2.widget || {};
  2. o2.widget.UUID = new Class({
  3. initialize: function(){
  4. this.id = this.createUUID();
  5. },
  6. valueOf: function() {
  7. return this.id;
  8. },
  9. toString: function() {
  10. return this.id;
  11. },
  12. createUUID: function(){
  13. //
  14. // Loose interpretation of the specification DCE 1.1: Remote Procedure Call
  15. // described at
  16. // http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
  17. // since JavaScript doesn't allow access to internal systems, the last 48
  18. // bits
  19. // of the node section is made up using a series of random numbers (6 octets
  20. // long).
  21. //
  22. var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
  23. var dc = new Date();
  24. var t = dc.getTime() - dg.getTime();
  25. var tl = this.getIntegerBits(t, 0, 31);
  26. var tm = this.getIntegerBits(t, 32, 47);
  27. var thv = this.getIntegerBits(t, 48, 59) + '1'; // version 1, security version is 2
  28. var csar = this.getIntegerBits(this.rand(4095), 0, 7);
  29. var csl = this.getIntegerBits(this.rand(4095), 0, 7);
  30. // since detection of anything about the machine/browser is far to buggy,
  31. // include some more random numbers here
  32. // if NIC or an IP can be obtained reliably, that should be put in
  33. // here instead.
  34. var n = this.getIntegerBits(this.rand(8191), 0, 7)
  35. + this.getIntegerBits(this.rand(8191), 8, 15)
  36. + this.getIntegerBits(this.rand(8191), 0, 7)
  37. + this.getIntegerBits(this.rand(8191), 8, 15)
  38. + this.getIntegerBits(this.rand(8191), 0, 15); // this last number is two octets long
  39. return tl + tm + thv + csar + csl + n;
  40. },
  41. createTrueUUID: function(){
  42. //
  43. // Loose interpretation of the specification DCE 1.1: Remote Procedure Call
  44. // described at
  45. // http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
  46. // since JavaScript doesn't allow access to internal systems, the last 48
  47. // bits
  48. // of the node section is made up using a series of random numbers (6 octets
  49. // long).
  50. //
  51. var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
  52. var dc = new Date();
  53. var t = dc.getTime() - dg.getTime();
  54. var tl = this.getIntegerBits(t, 0, 31);
  55. var tm = this.getIntegerBits(t, 32, 47);
  56. var thv = this.getIntegerBits(t, 48, 59) + '1'; // version 1, security version is 2
  57. var csar = this.getIntegerBits(this.rand(4095), 0, 7);
  58. var csl = this.getIntegerBits(this.rand(4095), 0, 7);
  59. // since detection of anything about the machine/browser is far to buggy,
  60. // include some more random numbers here
  61. // if NIC or an IP can be obtained reliably, that should be put in
  62. // here instead.
  63. var n = this.getIntegerBits(this.rand(8191), 0, 7)
  64. + this.getIntegerBits(this.rand(8191), 8, 15)
  65. + this.getIntegerBits(this.rand(8191), 0, 7)
  66. + this.getIntegerBits(this.rand(8191), 8, 15)
  67. + this.getIntegerBits(this.rand(8191), 0, 15); // this last number is two octets long
  68. return tl +"-"+ tm +"-"+ thv +"-"+ csar + csl +"-"+ n;
  69. },
  70. getIntegerBits: function(val, start, end){
  71. var base16 = this.returnBase(val, 16);
  72. var quadArray = new Array();
  73. var quadString = '';
  74. var i = 0;
  75. for (i = 0; i < base16.length; i++) {
  76. quadArray.push(base16.substring(i, i + 1));
  77. }
  78. for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
  79. if (!quadArray[i] || quadArray[i] == '')
  80. quadString += '0';
  81. else
  82. quadString += quadArray[i];
  83. }
  84. return quadString;
  85. },
  86. returnBase: function(number, base) {
  87. return (number).toString(base).toUpperCase();
  88. },
  89. rand: function(max) {
  90. return Math.floor(Math.random() * (max + 1));
  91. }
  92. });