-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Labels
help wantedExtra attention is neededExtra attention is neededquestionFurther information is requestedFurther information is requested
Description
Describe the bug
I am unsure if this implementation is the same algorithm as the original.
To Reproduce
The entropy is computed differently:
cuid-java/src/main/java/io/github/thibaultmeyer/cuid/CUID.java
Lines 265 to 275 in c55aa79
| private static String createEntropy(final int length) { | |
| int primeNumber; | |
| final StringBuilder stringBuilder = new StringBuilder(length); | |
| while (stringBuilder.length() < length) { | |
| primeNumber = PRIME_NUMBER_ARRAY[Math.abs(Common.nextIntValue()) % PRIME_NUMBER_ARRAY.length]; | |
| stringBuilder.append(Integer.toString(primeNumber * Common.nextIntValue(), NUMBER_BASE)); | |
| } | |
| return stringBuilder.toString(); |
The hash is computed differently:
cuid-java/src/main/java/io/github/thibaultmeyer/cuid/CUID.java
Lines 283 to 292 in c55aa79
| private static String computeHash(final String content, final int saltLength) { | |
| final String salt = createEntropy(saltLength); | |
| try { | |
| return new BigInteger(MessageDigest.getInstance("SHA3-256").digest((content + salt).getBytes(StandardCharsets.UTF_8))) | |
| .toString(NUMBER_BASE); | |
| } catch (final NoSuchAlgorithmException exception) { | |
| throw new CUIDGenerationException(exception); | |
| } | |
| } |
The alphabet is accessed differently:
| final char firstLetter = CUIDv2.ALPHABET_ARRAY[Math.abs(Common.nextIntValue()) % CUIDv2.ALPHABET_ARRAY.length]; |
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
help wantedExtra attention is neededExtra attention is neededquestionFurther information is requestedFurther information is requested