Code One:/*! Math.uuid.js (v1.4) http://www.broofa.com mailto:[email protected] Copyright (c) Robert Kieffer Dual Licensed Under the MIT and GPL licenses. */* * Generate a random uuid. * * USAGE:MATH.UUID (length, radix) * Length-the desired number of characters * radix-the number of allowable Values for each character. * * EXAMPLES: *//No arguments-returns RFC4122, version 4 ID * >>> math.uuid () * "92329d39-6f5c-452 0-abfc-aab64544e172 "* *//One argument-returns ID of the specified length * >>> math.uuid (15)//1 5 character ID (default base=62) * "VCYDXGLTXRVZSTV" * *///arguments-returns ID of the specified length, and Radix. (Radix must be <=) * >>> Math.uuid (8, 2)//8 character ID (base=2) * "01001010" * >>> M Ath.uuid (8, Ten)//8 character ID (base=10) * "47473046" * >>> Math.uuid (8, +)//8 character ID (base=16) * "098f4d35" */(function () { Private array of chars to use var chars = ' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '. Split (' ‘); Math.uuid = function (len, radix) {var chars = chars, uuid = [], I; Radix = Radix | | Chars.length; if (len) {//Compact form for (i = 0; i < len; i++) uuid[i] = Chars[0 | Math.random () *radix]; } else {//rfc4122, version 4 form var R; rfc4122 requires these characters uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; UUID[14] = ' 4 '; Fill in random data. At i==19 set the high bits of clock sequence AS//per rfc4122, Sec. 4.1.5 for (i = 0; i <; i++) { if (!uuid[i]) {r = 0 | Math.random () *16; Uuid[i] = chars[(i = = 19)? (R & 0x3) | 0X8:R]; }}} return Uuid.join ("); }; A more performant, but slightly bulkier, rfc4122v4 solution. We boost performance//by minimizing calls to random () math.uUidfast = function () {var chars = chars, uuid = new Array (), rnd=0, R; for (var i = 0; i < i++) {if (i==8 | | i==13 | | i==18 | | i==23) {Uuid[i] = '-'; } else if (i==14) {uuid[i] = ' 4 '; } else {if (rnd <= 0x02) rnd = 0x2000000 + (math.random () *0x1000000) | r = rnd & 0xf; Rnd = Rnd >> 4; Uuid[i] = chars[(i = = 19)? (R & 0x3) | 0X8:R]; }} return Uuid.join ("); }; A more compact, but less performant, rfc4122v4 solution:Math.uuidCompact = function () {return ' xxxxxxxx-xxxx-4x Xx-yxxx-xxxxxxxxxxxx '. Replace (/[xy]/g, function (c) {var r = math.random () *16|0, v = c = = ' X ' r: (r&0x3|0x8); return v.tostring (16); }); }; })(); Call method: Math.uuid () code two://on creation of a UUID object, set it ' s initial value function uuid () {this.id = This.createuui D (); }//When asked. This Object is, lie and return it ' s value UUID.prototype.valueOf =function () {return this.id;}; UUID.prototype.toString = function () {return this.id;}; INSTANCE specific METHODS//UUID.prototype.createUUID = function () {////Loose interpretation of the Speci Fication DCE 1.1:remote Procedure call//since JavaScript doesn ' t allow access to internal systems, the last bits The of the node section was made up using a series of random numbers (6 octets long). var dg = new Date (1582, 10, 15, 0, 0, 0, 0); var dc = new Date (); var t = dc.gettime ()-dg.gettime (); var tl = uuid.getintegerbits (t,0,31); var TM = uuid.getintegerbits (t,32,47); var THV = uuid.getintegerbits (t,48,59) + ' 1 '; Version 1, security version is 2 var csar = Uuid.getintegerbits (Uuid.rand (4095), 0,7); var CSL = Uuid.getintegerbits (Uuid.rand (4095), 0,7); Since detection of anything about the Machine/browser are far from buggy,//include some more random numbers here If NIC or an IP can is obtained reliably,That should is put in//here instead. var n = uuid.getintegerbits (Uuid.rand (8191), 0,7) + uuid.getintegerbits (Uuid.rand (8191), 8,15) + UUI D.getintegerbits (Uuid.rand (8191), 0,7) + uuid.getintegerbits (Uuid.rand (8191), 8,15) + Uuid.getintege Rbits (Uuid.rand (8191), 0,15); This last number is a octets long return TL + TM + THV + CSAR + CSL + N; }; Certain bits from a very large integer, used to get the time//code information for the first part of a UU ID. Would return Zero's if there//aren ' t enough bits to shift the where it needs to. Uuid.getintegerbits = function (val,start,end) {var base16 = uuid.returnbase (val,16); var quadarray = new Array (); var quadstring = '; var i = 0; for (i=0;i<base16.length;i++) {Quadarray.push (base16.substring (i,i+1)); } for (I=math.floor (START/4); I<=math.floor (END/4); i++) {if (!quadarray[i] | | quadarray[i] = = ") quadstring + = ' 0 '; else quadstring + = Quadarray[i]; } return quadstring; }; Replaced from the original function to leverage the built in methods in//javascript. Thanks to Robert Kieffer for pointing the one out uuid.returnbase = function (number, base) {return (number). toString ( Base). toUpperCase (); }; Pick a random number within a range of numbers//int b rand (int a); where 0 <= b <= a uuid.rand = function (max) {return Math.floor (Math.random () * (max + 1));}; Calling method: UUID.prototype.createUUID ()
JavaScript generates UUID