[nodejs] UUID's in javascript

2012-08-06 Thread Ted Young
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 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-s

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Martin Cooper
On Mon, Aug 6, 2012 at 10:28 AM, Ted Young 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. -- Martin Cooper > https:/

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 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/no

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,

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Ted Young
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,

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 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 > suspi

Re: [nodejs] UUID's in javascript

2012-08-06 Thread Rick Waldron
On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn 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 wrote: > >> Thanks for th

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 wrote: > On Mon, Aug 6, 2012 at 6:28 PM, Mark Hahn wrote: > >> That isn't a hack. It is almo

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

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 somethi

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 wrote: > Ma

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" wrote: > That looks like I was responding to Mark Hahn's commnet about sorting; I > wasn't. > > Of

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.floo

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 br

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 wrote: > Isaac is correct. Date.now is pretty guessable from a security standpoint > and so is Math.random. I was suggesti