uuid.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. uuid.js - Version 0.2
  3. JavaScript Class to create a UUID like identifier
  4. */
  5. // On creation of a UUID object, set it's initial value
  6. function UUID(){
  7. this.id = this.createUUID();
  8. }
  9. // When asked what this Object is, lie and return it's value
  10. UUID.prototype.valueOf = function(){ return this.id; }
  11. UUID.prototype.toString = function(){ return this.id; }
  12. //
  13. // INSTANCE SPECIFIC METHODS
  14. //
  15. UUID.prototype.createUUID = function(){
  16. //
  17. // Loose interpretation of the specification DCE 1.1: Remote Procedure Call
  18. // described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
  19. // since JavaScript doesn't allow access to internal systems, the last 48 bits
  20. // of the node section is made up using a series of random numbers (6 octets 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 h = '-';
  26. var tl = UUID.getIntegerBits(t,0,31);
  27. var tm = UUID.getIntegerBits(t,32,47);
  28. var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
  29. var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
  30. var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);
  31. // since detection of anything about the machine/browser is far to buggy,
  32. // include some more random numbers here
  33. // if NIC or an IP can be obtained reliably, that should be put in
  34. // here instead.
  35. var n = UUID.getIntegerBits(UUID.rand(8191),0,7) +
  36. UUID.getIntegerBits(UUID.rand(8191),8,15) +
  37. UUID.getIntegerBits(UUID.rand(8191),0,7) +
  38. UUID.getIntegerBits(UUID.rand(8191),8,15) +
  39. UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
  40. return tl + h + tm + h + thv + h + csar + csl + h + n;
  41. }
  42. //
  43. // GENERAL METHODS (Not instance specific)
  44. //
  45. // Pull out only certain bits from a very large integer, used to get the time
  46. // code information for the first part of a UUID. Will return zero's if there
  47. // aren't enough bits to shift where it needs to.
  48. UUID.getIntegerBits = function(val,start,end){
  49. var base16 = UUID.returnBase(val,16);
  50. var quadArray = new Array();
  51. var quadString = '';
  52. var i = 0;
  53. for(i=0;i<base16.length;i++){
  54. quadArray.push(base16.substring(i,i+1));
  55. }
  56. for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
  57. if(!quadArray[i] || quadArray[i] == '') quadString += '0';
  58. else quadString += quadArray[i];
  59. }
  60. return quadString;
  61. }
  62. // Numeric Base Conversion algorithm from irt.org
  63. // In base 16: 0=0, 5=5, 10=A, 15=F
  64. UUID.returnBase = function(number, base){
  65. //
  66. // Copyright 1996-2006 irt.org, All Rights Reserved.
  67. //
  68. // Downloaded from: http://www.irt.org/script/146.htm
  69. // modified to work in this class by Erik Giberti
  70. var convert = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  71. if (number < base) var output = convert[number];
  72. else {
  73. var MSD = '' + Math.floor(number / base);
  74. var LSD = number - MSD*base;
  75. if (MSD >= base) var output = this.returnBase(MSD,base) + convert[LSD];
  76. else var output = convert[MSD] + convert[LSD];
  77. }
  78. return output;
  79. }
  80. // pick a random number within a range of numbers
  81. // int b rand(int a); where 0 <= b <= a
  82. UUID.rand = function(max){
  83. return Math.floor(Math.random() * max);
  84. }
  85. // end of UUID class file