generate a uuid/guid

2,615 views
Skip to first unread message

billywhizz

unread,
Feb 8, 2010, 11:17:21 PM2/8/10
to nodejs
does anyone know if it's possible to generate a "genuine" guid/uuid in
node.js? from what i've read, this is not possible within the context
of javascript and would have to available somehow through the v8
engine or a node extension, or an internal node method (basically
something that calls out to c/c++).

thx

Marak Squires

unread,
Feb 8, 2010, 11:52:24 PM2/8/10
to nod...@googlegroups.com
you could do something like this maybe? 

randomString(64)
// randomString returns a pseude-random ASCII string which contains at least the specified number of bits of entropy
// the return value is a string of length ⌈bits/6⌉ of characters from the base64 alphabet
function randomString(bits){var chars,rand,i,ret
  chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  ret=''
  // in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
  while(bits > 0){
    rand=Math.floor(Math.random()*0x100000000) // 32-bit integer
    // base 64 means 6 bits per character, so we use the top 30 bits from rand to give 30/6=5 characters.
    for(i=26; i>0 && bits>0; i-=6, bits-=6) ret+=chars[0x3F & rand >>> i]}
  return ret}


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Dean Landolt

unread,
Feb 8, 2010, 11:54:32 PM2/8/10
to nod...@googlegroups.com
On Mon, Feb 8, 2010 at 11:52 PM, Marak Squires <marak....@gmail.com> wrote:
you could do something like this maybe? 

randomString(64)
// randomString returns a pseude-random ASCII string which contains at least the specified number of bits of entropy
// the return value is a string of length ⌈bits/6⌉ of characters from the base64 alphabet
function randomString(bits){var chars,rand,i,ret
  chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  ret=''
  // in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
  while(bits > 0){
    rand=Math.floor(Math.random()*0x100000000) // 32-bit integer
    // base 64 means 6 bits per character, so we use the top 30 bits from rand to give 30/6=5 characters.
    for(i=26; i>0 && bits>0; i-=6, bits-=6) ret+=chars[0x3F & rand >>> i]}
  return ret}


Or you could steal it from narwhal, which does something similar:

Benjamin Thomas

unread,
Feb 8, 2010, 11:59:12 PM2/8/10
to nod...@googlegroups.com

It depends on the UUID version you want:

http://en.wikipedia.org/wiki/Universally_Unique_Identifier

I'm guessing that by the way you wrote your question that you were
thinking something along the lines of version 1 or version 2 uuids.
Since version 4 is just made of random numbers it would certainly be
possible to generate a "genuine" version 4 uuid.

I think if you want one of the other versions you might have to write
some C bindings to something available on the machine. But I don't
know about that.

billywhizz

unread,
Feb 9, 2010, 9:30:43 AM2/9/10
to nodejs
thanks folks. i stole the one from narwhal for now. i'll look at doing
a c/c++ binding at some stage. on my travels i found a nice wsse
library here: http://rvr.typepad.com/wind/2005/07/wsse_for_javasc.html,
which i have modified to create SOAP headers as well as HTTP 1.1
headers. I can have a look at making it into a lib for node if anyone
is interested...

On Feb 9, 4:59 am, Benjamin Thomas <bam.tho...@gmail.com> wrote:

John Wright

unread,
Mar 17, 2010, 1:27:32 PM3/17/10
to nodejs
Did you ever create a guid binding for node? I need one now too, :).
If not, I can do it.

Nikhil

unread,
Mar 17, 2010, 3:25:39 PM3/17/10
to nodejs

On Mar 17, 10:27 pm, John Wright <mrjjwri...@gmail.com> wrote:
> Did you ever create a guid binding for node?  I need one now too, :).
> If not, I can do it.


Here is a very simple binding which uses libuuid which is a part of
e2fsprogs,
so it should be available on every system

http://bitbucket.org/nikhilm/uuidjs

Hope it does what you need, since it generates only string uuids right
now.

-Nikhil

John Wright

unread,
Mar 17, 2010, 4:26:30 PM3/17/10
to nodejs
Thanks! I will take a look. Should be faster than doing this in JS.
String UUIDs are just fine.

On Mar 17, 1:25 pm, Nikhil <nsm.nik...@gmail.com> wrote:
> On Mar 17, 10:27 pm, John Wright <mrjjwri...@gmail.com> wrote:
>

> > Did you ever create aguidbinding for node?  I need one now too, :).

Zachary Zolton

unread,
Mar 17, 2010, 2:10:34 PM3/17/10
to nod...@googlegroups.com
I have done something like this on Linux and Mac OS X:

sys.exec("uuidgen", function(err, stdout, stderr) {
sys.puts(stdout);
});

Robert Kieffer

unread,
Mar 18, 2010, 12:24:29 AM3/18/10
to nodejs
Just out of curiousity, how many guid's do you need to generate?

Check out the Math.uuid.js script I wrote (link at top of this page):
http://www.broofa.com/2008/09/javascript-uuid-function/

The script provides three separate implementations, all standalone.
And the test page includes a performance test that you can run. On my
dinky lil' MacBook the Math.uuidFast method does ~100K guids/second.

- rwk

On Mar 17, 11:10 am, Zachary Zolton <zachary.zol...@gmail.com> wrote:
> I have done something like this on Linux and Mac OS X:
>
> sys.exec("uuidgen", function(err, stdout, stderr) {
>   sys.puts(stdout);
>
> });

John Wright

unread,
Mar 18, 2010, 6:38:35 PM3/18/10
to nodejs
Ok your code was faster relatively for me. I am generating 250000
uuids and doing lots of other stuff, like storing the uuid in a
database. Math.uuidFast was relatively about 10 to 15% faster than
uuidjs (a native C++ node plugin). I am no uuid expert and my tests
were really informal but what I see is good enough for me. Thanks
Robert!

On Mar 17, 10:24 pm, Robert Kieffer <bro...@gmail.com> wrote:
> Just out of curiousity, how manyguid'sdo you need to generate?


>
> Check out the Math.uuid.js script I wrote (link at top of this page):http://www.broofa.com/2008/09/javascript-uuid-function/
>
> The script provides three separate implementations, all standalone.
> And the test page includes a performance test that you can run.  On my
> dinky lil' MacBook the Math.uuidFast method does ~100K guids/second.
>
> - rwk
>
> On Mar 17, 11:10 am, Zachary Zolton <zachary.zol...@gmail.com> wrote:
>
>
>
> > I have done something like this on Linux and Mac OS X:
>
> > sys.exec("uuidgen", function(err, stdout, stderr) {
> >   sys.puts(stdout);
>
> > });
> > On Wed, Mar 17, 2010 at 12:27 PM, John Wright <mrjjwri...@gmail.com> wrote:

> > > Did you ever create aguidbinding for node?  I need one now too, :).

Reply all
Reply to author
Forward
0 new messages