| 1 | /* |
|---|
| 2 | uuid.js - Version 0.3 |
|---|
| 3 | JavaScript Class to create a UUID like identifier |
|---|
| 4 | |
|---|
| 5 | Copyright (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved. |
|---|
| 6 | |
|---|
| 7 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 8 | the terms of the GNU General Public License as published by the Free Software |
|---|
| 9 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 10 | version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
|---|
| 14 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 15 | |
|---|
| 16 | You should have received a copy of the GNU General Public License along with |
|---|
| 17 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 18 | Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | |
|---|
| 20 | The latest version of this file can be downloaded from |
|---|
| 21 | http://www.af-design.com/resources/javascript_uuid.php |
|---|
| 22 | |
|---|
| 23 | HISTORY: |
|---|
| 24 | 6/5/06 - Initial Release |
|---|
| 25 | 5/22/08 - Updated code to run faster, removed randrange(min,max) in favor of |
|---|
| 26 | a simpler rand(max) function. Reduced overhead by using getTime() |
|---|
| 27 | method of date class (suggestion by James Hall). |
|---|
| 28 | 9/5/08 - Fixed a bug with rand(max) and additional efficiencies pointed out |
|---|
| 29 | by Robert Kieffer http://broofa.com/ |
|---|
| 30 | |
|---|
| 31 | KNOWN ISSUES: |
|---|
| 32 | - Still no way to get MAC address in JavaScript |
|---|
| 33 | - Research into other versions of UUID show promising possibilities |
|---|
| 34 | (more research needed) |
|---|
| 35 | - Documentation needs improvement |
|---|
| 36 | |
|---|
| 37 | */ |
|---|
| 38 | |
|---|
| 39 | // On creation of a UUID object, set it's initial value |
|---|
| 40 | function UUID(){ |
|---|
| 41 | this.id = this.createUUID(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | // When asked what this Object is, lie and return it's value |
|---|
| 45 | UUID.prototype.valueOf = function(){ return this.id; } |
|---|
| 46 | UUID.prototype.toString = function(){ return this.id; } |
|---|
| 47 | |
|---|
| 48 | // |
|---|
| 49 | // INSTANCE SPECIFIC METHODS |
|---|
| 50 | // |
|---|
| 51 | |
|---|
| 52 | UUID.prototype.createUUID = function(){ |
|---|
| 53 | // |
|---|
| 54 | // Loose interpretation of the specification DCE 1.1: Remote Procedure Call |
|---|
| 55 | // described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37 |
|---|
| 56 | // since JavaScript doesn't allow access to internal systems, the last 48 bits |
|---|
| 57 | // of the node section is made up using a series of random numbers (6 octets long). |
|---|
| 58 | // |
|---|
| 59 | var dg = new Date(1582, 10, 15, 0, 0, 0, 0); |
|---|
| 60 | var dc = new Date(); |
|---|
| 61 | var t = dc.getTime() - dg.getTime(); |
|---|
| 62 | var h = '-'; |
|---|
| 63 | var tl = UUID.getIntegerBits(t,0,31); |
|---|
| 64 | var tm = UUID.getIntegerBits(t,32,47); |
|---|
| 65 | var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2 |
|---|
| 66 | var csar = UUID.getIntegerBits(UUID.rand(4095),0,7); |
|---|
| 67 | var csl = UUID.getIntegerBits(UUID.rand(4095),0,7); |
|---|
| 68 | |
|---|
| 69 | // since detection of anything about the machine/browser is far to buggy, |
|---|
| 70 | // include some more random numbers here |
|---|
| 71 | // if NIC or an IP can be obtained reliably, that should be put in |
|---|
| 72 | // here instead. |
|---|
| 73 | var n = UUID.getIntegerBits(UUID.rand(8191),0,7) + |
|---|
| 74 | UUID.getIntegerBits(UUID.rand(8191),8,15) + |
|---|
| 75 | UUID.getIntegerBits(UUID.rand(8191),0,7) + |
|---|
| 76 | UUID.getIntegerBits(UUID.rand(8191),8,15) + |
|---|
| 77 | UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long |
|---|
| 78 | return tl + h + tm + h + thv + h + csar + csl + h + n; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | // |
|---|
| 83 | // GENERAL METHODS (Not instance specific) |
|---|
| 84 | // |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | // Pull out only certain bits from a very large integer, used to get the time |
|---|
| 88 | // code information for the first part of a UUID. Will return zero's if there |
|---|
| 89 | // aren't enough bits to shift where it needs to. |
|---|
| 90 | UUID.getIntegerBits = function(val,start,end){ |
|---|
| 91 | var base16 = UUID.returnBase(val,16); |
|---|
| 92 | var quadArray = new Array(); |
|---|
| 93 | var quadString = ''; |
|---|
| 94 | var i = 0; |
|---|
| 95 | for(i=0;i<base16.length;i++){ |
|---|
| 96 | quadArray.push(base16.substring(i,i+1)); |
|---|
| 97 | } |
|---|
| 98 | for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){ |
|---|
| 99 | if(!quadArray[i] || quadArray[i] == '') quadString += '0'; |
|---|
| 100 | else quadString += quadArray[i]; |
|---|
| 101 | } |
|---|
| 102 | return quadString; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // Replaced from the original function to leverage the built in methods in |
|---|
| 106 | // JavaScript. Thanks to Robert Kieffer for pointing this one out |
|---|
| 107 | UUID.returnBase = function(number, base){ |
|---|
| 108 | return (number).toString(base).toUpperCase(); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // pick a random number within a range of numbers |
|---|
| 112 | // int b rand(int a); where 0 <= b <= a |
|---|
| 113 | UUID.rand = function(max){ |
|---|
| 114 | return Math.floor(Math.random() * (max + 1)); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | // end of UUID class file |
|---|