Re: [nodejs] UUID's in javascript

2012-08-07 Thread Ted Young
Axel, that's interesting.  I was noticing the same issue, mainly that I have to 
create uuid's in batches, in the browser, and Date.now() was not granular 
enough, so they were all getting the same prefix.  Didn't seem very useful.  
Seems like there's no way to create strong, fast uuid's in the browser, but 
probably good enough for what I'm doing.  I will run a test at some point to 
see how frequently collisions actually occur.

Ted

On Aug 7, 2012, at 7:28 AM, Axel Kittenberger axk...@gmail.com wrote:

 This is what I do to create compact 96 bit UIDs in a fast way. 4 bits
 are per 6 characters but thats ok for me.
 
 var uid = function() {
var mime 
 ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var ua   = [];
for(var a = 0; a  3; a++) {
var r32  = Math.floor(0x1 * Math.random());
for(var b = 0; b  6; b++) {
ua.push(mime[r32  0x3F]);
r32 = r32  6;
}
}
return ua.join('');
 };
 
 I considered adding Date(), but figured it doesnt help me at all for
 possible collisions generated in the very moment (from multiple hosts)
 It may vary on the use case but I can handle collisions well with UIDs
 generated long ago, but not with the ones created in parallell. So in
 that case the space taken by Date() is far better used by yet another
 random() entity. I also use this notation to save space rather than
 the standarized UID type 4 things.
 
 Yes it is also not cryptographically strong, but in my case that isn't
 an issue and speed is more important. Also the web clients need to be
 able to create UIDs and that cancels out a lot possibilities anyway
 that might be available in node.
 
 -- 
 Job Board: http://jobs.nodejs.org/
 Posting guidelines: 
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups nodejs group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en


Re: [nodejs] UUID's in javascript

2012-08-07 Thread Mark Hahn
My use of UUIDs as DB keys has nothing to do with security.  Just global
uniqueness.  The term UUID doesn't imply any security.

On Mon, Aug 6, 2012 at 9:29 PM, Tim Caswell t...@creationix.com wrote:

 Isaac is correct.  Date.now is pretty guessable from a security standpoint
 and so is Math.random.  I was suggesting it for cases where that's not an
 issue.
  On Aug 6, 2012 8:11 PM, Isaac Schlueter i...@izs.me wrote:

 That looks like I was responding to Mark Hahn's commnet about sorting; I
 wasn't.

 Of course it's fine to put the date on there *also*, but my point is
 merely that (Date.now() + Math.random()) is not sufficiently random to
 call it a uuid.

 On Mon, Aug 6, 2012 at 6:09 PM, Isaac Schlueter i...@izs.me wrote:
  Math.random() is not cryptographically secure entropy, and Date.now()
  is extremely guessable.  If you are worried about someone *wanting* to
  cause collisions, then that's not so great.
 
  If you don't care about the IETF, and you are writing your program in
  Node, then the simplest approach is something like:
 
  var uuid = crypto.randomBytes(24).toString('base64')
 
  To do it asynchronously (since the crypto ops are a little slow):
 
  crypto.randomBytes(24, function (er, bytes) {
var uuid = bytes.toString('base64')
  })
 
  Make the number bigger or smaller to adjust the amount of entropy.
 
  Note that IETF uuids are only 16 bytes of entropy, so if you do want
  IETF-style uuids, you can do this:
 
  var uuid = crypto.randomBytes(16).toString('hex')
  var ietfStyle = uuid.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/,
  '$1-$2-$3-$4-$5')
 
  I'm going to be moving npm to just use base64 random bytes for salt
  eventually, just never got around to it.
 
 
  On Mon, Aug 6, 2012 at 4:02 PM, Mark Hahn m...@hahnca.com wrote:
  The advantage of starting off with the time is that the UUIDs sort by
 order
  generated which is useful for DB ids.  Coding a time into base 36 is a
 bit
  dangerous though as it may not sort right at some time in the future.
  That
  time may be centuries from now, not sure.  Sorting by hex is known to
 be far
  in the future.
 
 
  On Mon, Aug 6, 2012 at 3:56 PM, Mark Hahn m...@hahnca.com wrote:
 
  FWIW, here is the code to exactly match couchdb.
 
  UUID = - '0' + (new Date().getTime() * 1e3).toString(16) +
  Math.floor(Math.random() * 1e18).toString(16)
 
 
  On Mon, Aug 6, 2012 at 3:43 PM, Rick Waldron waldron.r...@gmail.com
  wrote:
 
  On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:
 
  That isn't a hack.  It is almost a copy of what couchdb offers.  I
 use
  it all the time.
 
 
  One is an IETF (RFC) and the other isn't.
 
  http://www.ietf.org/rfc/rfc4122.txt
 
 
 
  On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org
  wrote:
 
  Thanks for the feedback, glad to know I found the defacto solution.
  Tim, that's funny, your hack is more or less what I started with,
 but I
  became suspicious that it was too easy somehow.  Maybe I'll switch
 back to
  that if file-size/performance becomes an issue (which it won't).
 
  Ted
 
  On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com
 wrote:
 
  Depending on how strict your requirements are, I often just use:
 
   Date.now.toString(36) + - + (Math.random() *
  0x1000).toString(36)
 
  Date.now is unique every ms and Math.random has a keyspace of
 2^32, so
  collisions are statistically impossible in most practical
  applications.
 
 
  On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron 
 waldron.r...@gmail.com
  wrote:
 
 
  On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:
 
  On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
 
  wrote:
 
  Hey y'all,
 
  So, I have a need for global unique id's. Googling about, I've
 found
  the
  following implementations that seem decent:
 
  https://github.com/broofa/node-uuid
 
 
  FWIW, this is the package that npm uses.
 
  +1
 
  I've been using Robert Keiffer's impl in projects since before it
 was
  even
  on npm—I consider it a go to
 
  Rick
 
 
  --
  Martin Cooper
 
 
  https://gist.github.com/1308368
 
  Not really sure why I would pick one over the other (besides file
  size), or
  if there are any js-specific issues I should be aware of. Any
 advice?
 
  Cheers,
 
  Ted
 
  --
  Job Board: http://jobs.nodejs.org/
  Posting guidelines:
 
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
  You received this message because you are subscribed to the Google
  Groups nodejs group.
  To post to this group, send email to nodejs@googlegroups.com
  To unsubscribe from this group, send email to
  nodejs+unsubscr...@googlegroups.com
  For more options, visit this group at
  http://groups.google.com/group/nodejs?hl=en?hl=en
 
 
  --
  Job Board: http://jobs.nodejs.org/
  Posting guidelines:
 
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
  You received this message because you are subscribed to the Google
  Groups nodejs group.
  To post to this group, send 

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Rick Waldron


On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org wrote:
  Hey y'all,
   
  So, I have a need for global unique id's. Googling about, I've found the
  following implementations that seem decent:
   
  https://github.com/broofa/node-uuid
  
 FWIW, this is the package that npm uses.
  
+1

I've been using Robert Keiffer's impl in projects since before it was even on 
npm—I consider it a go to

Rick
  
  
 --
 Martin Cooper
  
  
  https://gist.github.com/1308368
   
  Not really sure why I would pick one over the other (besides file size), or
  if there are any js-specific issues I should be aware of. Any advice?
   
  Cheers,
   
  Ted
   
  --
  Job Board: http://jobs.nodejs.org/
  Posting guidelines:
  https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
  You received this message because you are subscribed to the Google
  Groups nodejs group.
  To post to this group, send email to nodejs@googlegroups.com
  To unsubscribe from this group, send email to
  nodejs+unsubscr...@googlegroups.com
  For more options, visit this group at
  http://groups.google.com/group/nodejs?hl=en?hl=en
   
  
  
 --  
 Job Board: http://jobs.nodejs.org/
 Posting guidelines: 
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en
  
  


-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups nodejs group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en


Re: [nodejs] UUID's in javascript

2012-08-06 Thread Tim Caswell
Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() * 0x1000).toString(36)

Date.now is unique every ms and Math.random has a keyspace of 2^32, so
collisions are statistically impossible in most practical
applications.

On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com wrote:

 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file size), or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups nodejs group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en


Re: [nodejs] UUID's in javascript

2012-08-06 Thread Mark Hahn
That isn't a hack.  It is almost a copy of what couchdb offers.  I use it
all the time.

On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org wrote:

 Thanks for the feedback, glad to know I found the defacto solution.  Tim,
 that's funny, your hack is more or less what I started with, but I became
 suspicious that it was too easy somehow.  Maybe I'll switch back to that if
 file-size/performance becomes an issue (which it won't).

   Ted

 On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:

 Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() * 0x1000).toString(36)

 Date.now is unique every ms and Math.random has a keyspace of 2^32, so
 collisions are statistically impossible in most practical
 applications.

 On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com
 wrote:


 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file size), or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


  --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups nodejs group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en


Re: [nodejs] UUID's in javascript

2012-08-06 Thread Rick Waldron
On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:

 That isn't a hack.  It is almost a copy of what couchdb offers.  I use it
 all the time.


One is an IETF (RFC) and the other isn't.

http://www.ietf.org/rfc/rfc4122.txt



 On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org wrote:

 Thanks for the feedback, glad to know I found the defacto solution.  Tim,
 that's funny, your hack is more or less what I started with, but I became
 suspicious that it was too easy somehow.  Maybe I'll switch back to that if
 file-size/performance becomes an issue (which it won't).

   Ted

 On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:

 Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() * 0x1000).toString(36)

 Date.now is unique every ms and Math.random has a keyspace of 2^32, so
 collisions are statistically impossible in most practical
 applications.


 On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com
 wrote:


 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
 wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file size),
 or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


   --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


  --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups nodejs group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en


Re: [nodejs] UUID's in javascript

2012-08-06 Thread Mark Hahn
FWIW, here is the code to exactly match couchdb.

UUID = - '0' + (new Date().getTime() * 1e3).toString(16) +
Math.floor(Math.random() * 1e18).toString(16)


On Mon, Aug 6, 2012 at 3:43 PM, Rick Waldron waldron.r...@gmail.com wrote:

 On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:

 That isn't a hack.  It is almost a copy of what couchdb offers.  I use it
 all the time.


 One is an IETF (RFC) and the other isn't.

 http://www.ietf.org/rfc/rfc4122.txt



 On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org wrote:

 Thanks for the feedback, glad to know I found the defacto solution.
  Tim, that's funny, your hack is more or less what I started with, but I
 became suspicious that it was too easy somehow.  Maybe I'll switch back to
 that if file-size/performance becomes an issue (which it won't).

   Ted

 On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:

 Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() * 0x1000).toString(36)

 Date.now is unique every ms and Math.random has a keyspace of 2^32, so
 collisions are statistically impossible in most practical
 applications.


 On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com
 wrote:


 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
 wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was
 even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file size),
 or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


   --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


  --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


  --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to 

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Mark Hahn
The advantage of starting off with the time is that the UUIDs sort by order
generated which is useful for DB ids.  Coding a time into base 36 is a bit
dangerous though as it may not sort right at some time in the future.  That
time may be centuries from now, not sure.  Sorting by hex is known to be
far in the future.

On Mon, Aug 6, 2012 at 3:56 PM, Mark Hahn m...@hahnca.com wrote:

 FWIW, here is the code to exactly match couchdb.

 UUID = - '0' + (new Date().getTime() * 1e3).toString(16) +
 Math.floor(Math.random() * 1e18).toString(16)


 On Mon, Aug 6, 2012 at 3:43 PM, Rick Waldron waldron.r...@gmail.comwrote:

 On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:

 That isn't a hack.  It is almost a copy of what couchdb offers.  I use
 it all the time.


 One is an IETF (RFC) and the other isn't.

 http://www.ietf.org/rfc/rfc4122.txt



 On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.orgwrote:

 Thanks for the feedback, glad to know I found the defacto solution.
  Tim, that's funny, your hack is more or less what I started with, but I
 became suspicious that it was too easy somehow.  Maybe I'll switch back to
 that if file-size/performance becomes an issue (which it won't).

   Ted

 On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:

 Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() * 0x1000).toString(36)

 Date.now is unique every ms and Math.random has a keyspace of 2^32, so
 collisions are statistically impossible in most practical
 applications.


 On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com
 wrote:


 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
 wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was
 even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file
 size), or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


   --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


  --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Isaac Schlueter
Math.random() is not cryptographically secure entropy, and Date.now()
is extremely guessable.  If you are worried about someone *wanting* to
cause collisions, then that's not so great.

If you don't care about the IETF, and you are writing your program in
Node, then the simplest approach is something like:

var uuid = crypto.randomBytes(24).toString('base64')

To do it asynchronously (since the crypto ops are a little slow):

crypto.randomBytes(24, function (er, bytes) {
  var uuid = bytes.toString('base64')
})

Make the number bigger or smaller to adjust the amount of entropy.

Note that IETF uuids are only 16 bytes of entropy, so if you do want
IETF-style uuids, you can do this:

var uuid = crypto.randomBytes(16).toString('hex')
var ietfStyle = uuid.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/,
'$1-$2-$3-$4-$5')

I'm going to be moving npm to just use base64 random bytes for salt
eventually, just never got around to it.


On Mon, Aug 6, 2012 at 4:02 PM, Mark Hahn m...@hahnca.com wrote:
 The advantage of starting off with the time is that the UUIDs sort by order
 generated which is useful for DB ids.  Coding a time into base 36 is a bit
 dangerous though as it may not sort right at some time in the future.  That
 time may be centuries from now, not sure.  Sorting by hex is known to be far
 in the future.


 On Mon, Aug 6, 2012 at 3:56 PM, Mark Hahn m...@hahnca.com wrote:

 FWIW, here is the code to exactly match couchdb.

 UUID = - '0' + (new Date().getTime() * 1e3).toString(16) +
 Math.floor(Math.random() * 1e18).toString(16)


 On Mon, Aug 6, 2012 at 3:43 PM, Rick Waldron waldron.r...@gmail.com
 wrote:

 On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:

 That isn't a hack.  It is almost a copy of what couchdb offers.  I use
 it all the time.


 One is an IETF (RFC) and the other isn't.

 http://www.ietf.org/rfc/rfc4122.txt



 On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org
 wrote:

 Thanks for the feedback, glad to know I found the defacto solution.
 Tim, that's funny, your hack is more or less what I started with, but I
 became suspicious that it was too easy somehow.  Maybe I'll switch back to
 that if file-size/performance becomes an issue (which it won't).

 Ted

 On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:

 Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() *
 0x1000).toString(36)

 Date.now is unique every ms and Math.random has a keyspace of 2^32, so
 collisions are statistically impossible in most practical
 applications.


 On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com
 wrote:


 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
 wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found
 the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was
 even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file
 size), or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send 

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Isaac Schlueter
That looks like I was responding to Mark Hahn's commnet about sorting; I wasn't.

Of course it's fine to put the date on there *also*, but my point is
merely that (Date.now() + Math.random()) is not sufficiently random to
call it a uuid.

On Mon, Aug 6, 2012 at 6:09 PM, Isaac Schlueter i...@izs.me wrote:
 Math.random() is not cryptographically secure entropy, and Date.now()
 is extremely guessable.  If you are worried about someone *wanting* to
 cause collisions, then that's not so great.

 If you don't care about the IETF, and you are writing your program in
 Node, then the simplest approach is something like:

 var uuid = crypto.randomBytes(24).toString('base64')

 To do it asynchronously (since the crypto ops are a little slow):

 crypto.randomBytes(24, function (er, bytes) {
   var uuid = bytes.toString('base64')
 })

 Make the number bigger or smaller to adjust the amount of entropy.

 Note that IETF uuids are only 16 bytes of entropy, so if you do want
 IETF-style uuids, you can do this:

 var uuid = crypto.randomBytes(16).toString('hex')
 var ietfStyle = uuid.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/,
 '$1-$2-$3-$4-$5')

 I'm going to be moving npm to just use base64 random bytes for salt
 eventually, just never got around to it.


 On Mon, Aug 6, 2012 at 4:02 PM, Mark Hahn m...@hahnca.com wrote:
 The advantage of starting off with the time is that the UUIDs sort by order
 generated which is useful for DB ids.  Coding a time into base 36 is a bit
 dangerous though as it may not sort right at some time in the future.  That
 time may be centuries from now, not sure.  Sorting by hex is known to be far
 in the future.


 On Mon, Aug 6, 2012 at 3:56 PM, Mark Hahn m...@hahnca.com wrote:

 FWIW, here is the code to exactly match couchdb.

 UUID = - '0' + (new Date().getTime() * 1e3).toString(16) +
 Math.floor(Math.random() * 1e18).toString(16)


 On Mon, Aug 6, 2012 at 3:43 PM, Rick Waldron waldron.r...@gmail.com
 wrote:

 On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:

 That isn't a hack.  It is almost a copy of what couchdb offers.  I use
 it all the time.


 One is an IETF (RFC) and the other isn't.

 http://www.ietf.org/rfc/rfc4122.txt



 On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org
 wrote:

 Thanks for the feedback, glad to know I found the defacto solution.
 Tim, that's funny, your hack is more or less what I started with, but I
 became suspicious that it was too easy somehow.  Maybe I'll switch back 
 to
 that if file-size/performance becomes an issue (which it won't).

 Ted

 On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:

 Depending on how strict your requirements are, I often just use:

  Date.now.toString(36) + - + (Math.random() *
 0x1000).toString(36)

 Date.now is unique every ms and Math.random has a keyspace of 2^32, so
 collisions are statistically impossible in most practical
 applications.


 On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron waldron.r...@gmail.com
 wrote:


 On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:

 On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
 wrote:

 Hey y'all,

 So, I have a need for global unique id's. Googling about, I've found
 the
 following implementations that seem decent:

 https://github.com/broofa/node-uuid


 FWIW, this is the package that npm uses.

 +1

 I've been using Robert Keiffer's impl in projects since before it was
 even
 on npm—I consider it a go to

 Rick


 --
 Martin Cooper


 https://gist.github.com/1308368

 Not really sure why I would pick one over the other (besides file
 size), or
 if there are any js-specific issues I should be aware of. Any advice?

 Cheers,

 Ted

 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/nodejs?hl=en?hl=en


 --
 Job Board: http://jobs.nodejs.org/
 Posting guidelines:
 https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
 You received this message because you are subscribed to the Google
 Groups nodejs group.
 To post to this group, send email to nodejs@googlegroups.com
 To unsubscribe from this group, send email to
 nodejs+unsubscr...@googlegroups.com
 For more 

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Tim Caswell
Isaac is correct.  Date.now is pretty guessable from a security standpoint
and so is Math.random.  I was suggesting it for cases where that's not an
issue.
On Aug 6, 2012 8:11 PM, Isaac Schlueter i...@izs.me wrote:

 That looks like I was responding to Mark Hahn's commnet about sorting; I
 wasn't.

 Of course it's fine to put the date on there *also*, but my point is
 merely that (Date.now() + Math.random()) is not sufficiently random to
 call it a uuid.

 On Mon, Aug 6, 2012 at 6:09 PM, Isaac Schlueter i...@izs.me wrote:
  Math.random() is not cryptographically secure entropy, and Date.now()
  is extremely guessable.  If you are worried about someone *wanting* to
  cause collisions, then that's not so great.
 
  If you don't care about the IETF, and you are writing your program in
  Node, then the simplest approach is something like:
 
  var uuid = crypto.randomBytes(24).toString('base64')
 
  To do it asynchronously (since the crypto ops are a little slow):
 
  crypto.randomBytes(24, function (er, bytes) {
var uuid = bytes.toString('base64')
  })
 
  Make the number bigger or smaller to adjust the amount of entropy.
 
  Note that IETF uuids are only 16 bytes of entropy, so if you do want
  IETF-style uuids, you can do this:
 
  var uuid = crypto.randomBytes(16).toString('hex')
  var ietfStyle = uuid.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/,
  '$1-$2-$3-$4-$5')
 
  I'm going to be moving npm to just use base64 random bytes for salt
  eventually, just never got around to it.
 
 
  On Mon, Aug 6, 2012 at 4:02 PM, Mark Hahn m...@hahnca.com wrote:
  The advantage of starting off with the time is that the UUIDs sort by
 order
  generated which is useful for DB ids.  Coding a time into base 36 is a
 bit
  dangerous though as it may not sort right at some time in the future.
  That
  time may be centuries from now, not sure.  Sorting by hex is known to
 be far
  in the future.
 
 
  On Mon, Aug 6, 2012 at 3:56 PM, Mark Hahn m...@hahnca.com wrote:
 
  FWIW, here is the code to exactly match couchdb.
 
  UUID = - '0' + (new Date().getTime() * 1e3).toString(16) +
  Math.floor(Math.random() * 1e18).toString(16)
 
 
  On Mon, Aug 6, 2012 at 3:43 PM, Rick Waldron waldron.r...@gmail.com
  wrote:
 
  On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn m...@hahnca.com wrote:
 
  That isn't a hack.  It is almost a copy of what couchdb offers.  I
 use
  it all the time.
 
 
  One is an IETF (RFC) and the other isn't.
 
  http://www.ietf.org/rfc/rfc4122.txt
 
 
 
  On Mon, Aug 6, 2012 at 2:17 PM, Ted Young t...@radicaldesigns.org
  wrote:
 
  Thanks for the feedback, glad to know I found the defacto solution.
  Tim, that's funny, your hack is more or less what I started with,
 but I
  became suspicious that it was too easy somehow.  Maybe I'll switch
 back to
  that if file-size/performance becomes an issue (which it won't).
 
  Ted
 
  On Aug 6, 2012, at 8:27 PM, Tim Caswell t...@creationix.com wrote:
 
  Depending on how strict your requirements are, I often just use:
 
   Date.now.toString(36) + - + (Math.random() *
  0x1000).toString(36)
 
  Date.now is unique every ms and Math.random has a keyspace of 2^32,
 so
  collisions are statistically impossible in most practical
  applications.
 
 
  On Mon, Aug 6, 2012 at 1:01 PM, Rick Waldron 
 waldron.r...@gmail.com
  wrote:
 
 
  On Monday, August 6, 2012 at 1:38 PM, Martin Cooper wrote:
 
  On Mon, Aug 6, 2012 at 10:28 AM, Ted Young t...@radicaldesigns.org
  wrote:
 
  Hey y'all,
 
  So, I have a need for global unique id's. Googling about, I've found
  the
  following implementations that seem decent:
 
  https://github.com/broofa/node-uuid
 
 
  FWIW, this is the package that npm uses.
 
  +1
 
  I've been using Robert Keiffer's impl in projects since before it
 was
  even
  on npm—I consider it a go to
 
  Rick
 
 
  --
  Martin Cooper
 
 
  https://gist.github.com/1308368
 
  Not really sure why I would pick one over the other (besides file
  size), or
  if there are any js-specific issues I should be aware of. Any
 advice?
 
  Cheers,
 
  Ted
 
  --
  Job Board: http://jobs.nodejs.org/
  Posting guidelines:
  https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
  You received this message because you are subscribed to the Google
  Groups nodejs group.
  To post to this group, send email to nodejs@googlegroups.com
  To unsubscribe from this group, send email to
  nodejs+unsubscr...@googlegroups.com
  For more options, visit this group at
  http://groups.google.com/group/nodejs?hl=en?hl=en
 
 
  --
  Job Board: http://jobs.nodejs.org/
  Posting guidelines:
  https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
  You received this message because you are subscribed to the Google
  Groups nodejs group.
  To post to this group, send email to nodejs@googlegroups.com
  To unsubscribe from this group, send email to
  nodejs+unsubscr...@googlegroups.com
  For more options, visit this group at
  http://groups.google.com/group/nodejs?hl=en?hl=en
 

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Axel Kittenberger
This is what I do to create compact 96 bit UIDs in a fast way. 4 bits
are per 6 characters but thats ok for me.

var uid = function() {
var mime 
='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var ua   = [];
for(var a = 0; a  3; a++) {
var r32  = Math.floor(0x1 * Math.random());
for(var b = 0; b  6; b++) {
ua.push(mime[r32  0x3F]);
r32 = r32  6;
}
}
return ua.join('');
};

I considered adding Date(), but figured it doesnt help me at all for
possible collisions generated in the very moment (from multiple hosts)
It may vary on the use case but I can handle collisions well with UIDs
generated long ago, but not with the ones created in parallell. So in
that case the space taken by Date() is far better used by yet another
random() entity. I also use this notation to save space rather than
the standarized UID type 4 things.

Yes it is also not cryptographically strong, but in my case that isn't
an issue and speed is more important. Also the web clients need to be
able to create UIDs and that cancels out a lot possibilities anyway
that might be available in node.

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups nodejs group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en