UUID.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. getIntegerBits: function(val, start, end){
  42. var base16 = this.returnBase(val, 16);
  43. var quadArray = new Array();
  44. var quadString = '';
  45. var i = 0;
  46. for (i = 0; i < base16.length; i++) {
  47. quadArray.push(base16.substring(i, i + 1));
  48. }
  49. for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {
  50. if (!quadArray[i] || quadArray[i] == '')
  51. quadString += '0';
  52. else
  53. quadString += quadArray[i];
  54. }
  55. return quadString;
  56. },
  57. returnBase: function(number, base) {
  58. return (number).toString(base).toUpperCase();
  59. },
  60. rand: function(max) {
  61. return Math.floor(Math.random() * (max + 1));
  62. }
  63. });