Tuesday, January 16, 2007

Creating GUIDs with Client Side JavaScript

I needed to generate GUIDs (globally unique identifiers) for a web project I was working on.The only solutions I could see for that task involved evoking an ActiveX control which makes it really useless for general purpose. I ended up writing a short javascript that will achieve this task:

I'm not trying to be rude - just to help. This isn't a GUID generator. a GUID is a "Globally Unique Identifier", which means that if everybody in the world created one, then no two of the GUIDs would ever be the same.

Since this GUID is based only on a random number (and doesn't include say an IP address), two people could easily get the same GUID. Since Javascript's random number generator is based on the current time, all it would take is for two users to create a GUID at the same time of day (to the millisecond), which in any reasonably well used application isn't inconceivable.

Ryan,You make a vaild point. In general it is very hard to generate "real" random number due to the need for a seed that itself be - random. Now for this algorithm we use 32 random selections of digits to generate the number. With that said, even if those two machines you mentioned, both starting at the same time get the same digit they will have to be in complete sync in order to generate identical numbers which is very unlikely.There is always a chnnce of repitition but I belive that it is very low.

I've got a need for a guid on my clients, and I think I'm going to have to break down and make the server generate the guid. The reason lies in the difference between "Random" and "Unique." Generating random number will give you a low probability of having two clients use the same number - but it's not a zero probability. A real "guid" generated with a technique the ensures that number will never, ever be generated again is something that I think can only be done on a server which uses something like it's IP address + NIC card identifier and only generates them one at a time and uses the system time as part of the formula.

I have a similar problem...we need to create a *unique* GUID - we cannot even accept a low probability of two PCs having identical values. This is in part b/c if at some point they need to be regenerated, the exact same GUID must be generated for the same PC as the last time. And we need to do this client-side using either JS or Flash (unless someone has a better idea). Can anyone make any suggestions? It also needs to work cross-OS and cross-platform, so ActiveX is not an option. Thanks!

I have a similar problem...we need to create a *unique* GUID - we cannot even accept a low probability of two PCs having identical values. This is in part b/c if at some point they need to be regenerated, the exact same GUID must be generated for the same PC as the last time. And we need to do this client-side using either JS or Flash (unless someone has a better idea). Can anyone make any suggestions? It also needs to work cross-OS and cross-platform, so ActiveX is not an option. Thanks!

lgodiva,It sounds like what you need is not exactly a GUID, you need some kind a signiture that will globally identify a browser and then making sure that you will be able to recreate the same number again given that you encounter the same client. The idea with GUID generator is that you create a differne number every time you rin it. I think what you are looking for is some kind of a hash function that will give you a low probablility of collisions given that you met the same machine. This is not trivial as IPs change, you do not have the MAC address of the computer or anything that is globally identifiable, I am guessing things are done that way to protect privacy.The way I would resolve this is to generate a GUID(on the sever or on the client). When the client makes a request the GUID as cookie on the client. On future requests you will access the GUID by reading the cookie.

Digg

Limitation of Liability for using this code

In no event will the publisher of this site or writer of this blog be liable for any damages, including loss of data, lost profits, cost of cover, or other special, incidental, consequential, direct or indirect damages arising from the software or the use thereof, however caused and on any theory of liability. This limitation will apply even if the publisher of this site or writer of this blog has been advised of the possibility of such damage. You acknowledge that this is a reasonable allocation of risk.