ai / nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
https://zelark.github.io/nano-id-cc/
MIT License
24.36k stars 790 forks source link

uuid5 like namespace feature #112

Closed sweepy84 closed 5 years ago

sweepy84 commented 5 years ago

Hi - I'm looking to use user-friendly short ids that are about 10 characters long and want to reduce the probability of collisions by using namespaces. To be honest I'm not entire sure I understand how namepsaces are used in uuid5 but based on what I understand it ensures a level of uniqueness across "buckets" or namespaces.

I guess I can essentially do the same thing by hashing a namespace and prepending it to the ID but was wondering if there is such a feature built into nanoid.

Something like a salt function to ensure uniqueness across salt strings.

thanks!

ai commented 5 years ago

Nano ID doesn't have built-in namespaces (it is easy to implement then when you have long ID like in UUID, but in 10 symbols ID you can't have universal implementation, you need to know use case and limits).

In my project I just put namespace directly to ID without hashing user.id + ':' + nanoid(7) to be able to unpack user ID from record ID.

ai commented 5 years ago

Something like a salt function to ensure uniqueness across salt strings.

Salt doesn't garantee uniqness. And with salt you need to use software RNG which has bigger collisions probability than hardware.