thibaultmeyer / cuid-java

Collision-resistant ids optimized for horizontal scaling and performance
MIT License
34 stars 0 forks source link

Really the Cuid2 algorithm? #7

Open sdavids opened 1 month ago

sdavids commented 1 month ago

Describe the bug

I am unsure if this implementation is the same algorithm as the original.

To Reproduce

The entropy is computed differently:

https://github.com/paralleldrive/cuid2/blob/53e246b0919c8123e492e6b6bbab41fe66f4b462/src/index.js#L7-L14

https://github.com/thibaultmeyer/cuid-java/blob/c55aa797b13348a01ad1a059fe88a5af1c613e8f/src/main/java/io/github/thibaultmeyer/cuid/CUID.java#L265-L275


The hash is computed differently:

https://github.com/paralleldrive/cuid2/blob/53e246b0919c8123e492e6b6bbab41fe66f4b462/src/index.js#L31-L35

https://github.com/thibaultmeyer/cuid-java/blob/c55aa797b13348a01ad1a059fe88a5af1c613e8f/src/main/java/io/github/thibaultmeyer/cuid/CUID.java#L283-L292


The alphabet is accessed differently:

https://github.com/paralleldrive/cuid2/blob/53e246b0919c8123e492e6b6bbab41fe66f4b462/src/index.js#L37-L39

https://github.com/thibaultmeyer/cuid-java/blob/c55aa797b13348a01ad1a059fe88a5af1c613e8f/src/main/java/io/github/thibaultmeyer/cuid/CUID.java#L69

https://github.com/ai/nanoid?tab=readme-ov-file

random % alphabet is a popular mistake [...] [t]he distribution will not be even; there will be a lower chance for some symbols to appear compared to others.

sdavids commented 1 month ago

@ericelliott Maybe you could provide an authoritative assessment?

sdavids commented 1 month ago

I opened this issue because I considered doing a 1:1 algorithm translation in a new repo but I think it would be better to update this project instead of creating a new one.

ericelliott commented 2 weeks ago

Correct that % is not a safe method to generate uniform random values. Are the histogram and randogram tests run in this repo?