source: trunk/src/ajax/javascripts/external/UUID.js

Last change on this file was 635, checked in by lgiessmann, 13 years ago

trunk: added the files textgrid_15.07.2011.xtm that contains the lates dump and test data of the TextGrid? ServiceRegistry?, and UUID.js, which contains a class definition for UUID => referenced it via the isidorus.html file

File size: 4.1 KB
Line 
1/*
2uuid.js - Version 0.3
3JavaScript Class to create a UUID like identifier
4 
5Copyright (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved.
6 
7This program is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2 of the License, or (at your option) any later
10version.
11 
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 
16You should have received a copy of the GNU General Public License along with
17this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18Place, Suite 330, Boston, MA 02111-1307 USA
19 
20The latest version of this file can be downloaded from
21http://www.af-design.com/resources/javascript_uuid.php
22 
23HISTORY:
246/5/06  - Initial Release
255/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).
289/5/08  - Fixed a bug with rand(max) and additional efficiencies pointed out
29          by Robert Kieffer http://broofa.com/
30 
31KNOWN 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
40function UUID(){
41        this.id = this.createUUID();
42}
43 
44// When asked what this Object is, lie and return it's value
45UUID.prototype.valueOf = function(){ return this.id; }
46UUID.prototype.toString = function(){ return this.id; }
47 
48//
49// INSTANCE SPECIFIC METHODS
50//
51 
52UUID.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.
90UUID.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
107UUID.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
113UUID.rand = function(max){
114        return Math.floor(Math.random() * (max + 1));
115}
116 
117// end of UUID class file
Note: See TracBrowser for help on using the repository browser.