Generate valid GUID’s / UUID’s with ASP/VBScript

01/27/2013 | By | 2 Replies More

I’ve been working on a new ASP website and needed a way to generate valid UUID’s.  I searched around a bit but all I could find for ASP were functions that just generate some hex and added a few dashes.  They probably would have worked fine but if you’re going to do something you, might as well try and do it right.

Maybe this will save someone else some time.  I wrote this function which generates a valid version 4 UUID (or GUID).  Version 4 is just the version that uses random numbers.  There are other versions based on timestamps and MD5 hashes but for my purpose this is all I needed. You can tell if a version 4 UUID is valid because the 13th digit will be a ‘4’ and the 17th digit will be a ‘8’, ‘9’, ‘a’ or ‘b’.

'UUID Generator for UUID Version 4 (Random)
'Ref: http://www.ietf.org/rfc/rfc4122.txt
'Sample UUID: 7d265bf8-931c-42dc-8417-9ef3c2bbaa4e
Randomize Timer
Function UUID()
   Dim i, RndNum
   For i = 0 to 7
      RndNum = CLng(rnd * "&HFFFF")
     'if i = 0 then RndNum = RndNum Xor (CLng(Date) And "&HFFFF")
      If i = 3 Then RndNum = (RndNum And "&HFFF") Or "&H4000"
      If i = 4 Then RndNum = (RndNum And "&H3FFF") Or "&H8000"
      UUID = UUID + String(4 - Len(Hex(RndNum)), "0") + LCase(Hex(RndNum))
      If i=1 Or i=2 Or i=3 Or i=4 Then UUID = UUID + "-"
   Next
End Function

The spec says “The hexadecimal values ‘a’ through ‘f’ are output as lower case characters and are case insensitive on input.” Not sure why it matters but thats why I made them lower case.

In case you didn’t know a GUID and a UUID are the same thing. GUID is a microsoft term for UUID. After reading through the spec it looks like the microsoft version has a 5 in the variant field instead of a 4 so you would have to change the “&H8000” in the function to a “&HC000”. I’m not sure if there are other changes.

I feel that it’s worth mentioning that the Timer seed I used for the Randomize statement will return the number of seconds since midnight with 2 decimal places (example: 29697.71). If you are constantly generating a lot of UUID’s you may have to worry about using the same seed more than once. This is unlikely but made much more likely if generating them at a specific time each day. If a seed is used more than once the same UUID’s could will be generated both times. If you’re concerned about this then you may want to find something else to use as the seed, or you might need to keep track of the seeds that have already been used and make sure you don’t use them a second time. If you look at the function you will see that I commented out a line. If you uncomment this line it will XOR the first 16 bits of the UUID with the date. This is not part of the UUID spec which is why I left it commented but it should help prevent duplicates caused by using the same seed on different days. This way at least a couple of bits would be different.

The VBS random number generator is not known for being the best. It is said that it should not be used for cryptography or password generation. I think UUID’s fall into the same category. My function will create valid UUID’s but it uses the VBS random number generator so keep that in mind. There are other random number generators out there that you can use. I found this one on Google which is said to be better although I have no personal experience with it. 32-bit Multiply With Carry generator

Category: Classic ASP, Internet

About the Author ()

Comments (2)

Trackback URL | Comments RSS Feed

  1. Vikram says:

    I generated 2000 ramdom IDs using your code provided above, and when I put it into excel and removed duplicates, it showed 128 unique Ids. How is that called UUID when there is possibility of 1872 duplicates out of 2000

    • Chris says:

      I screwed up while copy and pasting and had the Randomize Timer line inside the function. That line should only be ran once per script, not each time a UUID is generated. It should be OK now.

Leave a Reply